从多个版本记录中保留最新版本
* 二层分支的情况:
* SELECT obj FROM ZxlPrdRequestModule obj
* LEFT JOIN ZxlPrdProductBranchFilter ppbf ON ppbf.hid=obj.hid
* AND ppbf.branchProductId IN :productIdList
* WHERE ( (obj.zxlPrdId=:zxlPrdId AND obj.active=1)
* OR (obj.zxlPrdId=:baseProductId1 AND obj.versionId<=:baseProductVersionId1 AND ppbf.active IS NULL)
* OR (obj.zxlPrdId=:baseProductId2 AND obj.versionId<=:baseProductVersionId2 AND ppbf.active IS NULL) )
* AND NOT (obj.editOpType='DELETE' AND obj.editOpStatus='CONFIRMED')
*
* AND NOT EXISTS(SELECT 1 FROM ZxlPrdRequestModule _obj
* WHERE ( (_obj.zxlPrdId=:zxlPrdId AND _obj.active=1)
* OR (_obj.zxlPrdId=:baseProductId1 AND _obj.versionId<=:baseProductVersionId1 AND ppbf.active IS NULL)
* OR (_obj.zxlPrdId=:baseProductId2 AND _obj.versionId<=:baseProductVersionId2 AND ppbf.active IS NULL) )
* AND NOT (_obj.editOpType='DELETE' AND _obj.editOpStatus='CONFIRMED')
* AND obj.moduleId=_obj.moduleId AND obj.versionId<_obj.versionId)
=========================下面内容转载=========================
SQL中经常遇到如下情况,在一张表中有两条记录基本完全一样,某个或某几个字段有些许差别, 这时候可能需要我们踢出这些有差别的数据,即两条或多条记录中只保留一项。
如下:表timeand
针对time字段相同时有不同total和name的情形,每当遇到相同的则只取其中一条数据,最简单的实现方法有两种
每当遇到相同的则只取其中一条数据,最简单的实现方法有两种
1、select time,max(total) as total,name from timeand group by time;//取记录中total最大的值
或 select time,min(total) as total,name from timeand group by time;//取记录中total最小的值
上述两种方案都有个缺点,就是无法区分name字段的内容,所以一般用于只有两条字段或其他字段内容完全一致的情况
2、select * from timeand as a where not exists(select 1 from timeand where a.time = time and a.total<total);
此中方案排除了方案1中name字段不准确的问题,取的是total最大的值
---