sql中去除重复的数据

总的思路就是先找出表中重复数据中的一条数据,插入临时表中,删除所有的重复数据,然后再将临时表中的数据插入表中。所以重点是如何找出重复数据中的一条数据,有三种情况

1.重复数据完全一样,使用distinct

  select distinct * from table

2.id列不同,id类型为int,自增字段,使用聚合函数max或其他

select * from  table where id in(

  select MAX(id)

  FROM table  group by “分组字段”having COUNT(*)>1)

3.id列不同,id类型为uniqueidentifier

(1)使用row_number() over()和partition by给每一组添加行号

Select *,(row_number() Over(partition By ‘分组字段’Order BY ‘排序字段’)) RowNum From 
  (select * from  table where ‘分组字段’in(

  select ‘分组字段’  FROM table  group by “分组字段”having COUNT(*)>1) t1)

(2)将行号=1的数据插入临时表中

Select * into #A from (‘上面的sql语句’) t2 where t2.RowNum=1

注意:row_number() over()是给行加行号的

 partition  by用于给结果集分组,如果没有指定那么它把整个结果集作为一个分组,参考http://blog.csdn.net/wuzhengqing1/article/details/8024634

原文地址:https://www.cnblogs.com/zxking/p/6431400.html