遇到一个情况,主键为AccreditID的Accredit表中有错误数据,这些错误数据中CardID,ProductNumber,ProductEndTime字段都一样,现在需要删除这些错误数据,因为这样的数据针对于授权这个功能来讲,算是重复数据。
首先从直观来说,肯定要进行分组,分组的方式,当然是3个字段一起都作为分组字段,以逗号隔开,重复的数据必须是分组后数量都大于1的。于是,有了分组sql语句:
group by CardID,ProductNumber,ProductEndTime having COUNT(*)>1需要说的一点是,上面只是满足了CardID,ProductNumber,ProductEndTime字段都重复这个条件,没有满足“一样”,这里易被忽视。所以要分别对这3个字段做重复条件过滤,于是,结合上面的分组过滤数据集,有了下面的条件过滤sql语句:
where CardID in (select CardID from CAS.dbo.TotalAccredit group by CardID,ProductNumber,ProductEndTime having COUNT(*)>1)到了这里,获取到了所有重复数据,因为只需要保留最新的授权数据,于是还需要找到那些不是最新的重复数据。这里 比较重要的是,其实可以根据自增唯一主键AccreditID,来作为筛选过滤条件,选择不是最新的数据集。所以总的来说条件语句应该如下:
and ProductNumber in (select ProductNumber from CAS.dbo.TotalAccredit group by CardID,ProductNumber,ProductEndTime having COUNT(*)>1)
and ProductEndTime in (select ProductEndTime from CAS.dbo.TotalAccredit group by CardID,ProductNumber,ProductEndTime having COUNT(*)>1)
where CardID in (select CardID from CAS.dbo.TotalAccredit group by CardID,ProductNumber,ProductEndTime having COUNT(*)>1)
and ProductNumber in (select ProductNumber from CAS.dbo.TotalAccredit group by CardID,ProductNumber,ProductEndTime having COUNT(*)>1)
and ProductEndTime in (select ProductEndTime from CAS.dbo.TotalAccredit group by CardID,ProductNumber,ProductEndTime having COUNT(*)>1)
and AccreditID not in (select max(AccreditID) from CAS.dbo.TotalAccredit group by CardID,ProductNumber,ProductEndTime having COUNT(*)>1)
最后,需要删除这些数据,因此有了删除上面数据集的sql语句:
delete * from CAS.dbo.TotalAccredit当然,你也可以加上order by AccreditID作为排序,如果不是删除而是查询的话。
where CardID in (select CardID from CAS.dbo.TotalAccredit group by CardID,ProductNumber,ProductEndTime having COUNT(*)>1)
and ProductNumber in (select ProductNumber from CAS.dbo.TotalAccredit group by CardID,ProductNumber,ProductEndTime having COUNT(*)>1)
and ProductEndTime in (select ProductEndTime from CAS.dbo.TotalAccredit group by CardID,ProductNumber,ProductEndTime having COUNT(*)>1)
and AccreditID not in (select max(AccreditID) from CAS.dbo.TotalAccredit group by CardID,ProductNumber,ProductEndTime having COUNT(*)>1)