删除单表中重复的行

今天在数据表中发现有N多重复的数据,当时就奇怪了。于是找了方法将这些重复的数据删除。下面是操作方案,以免忘记!
1。先通过查询语句确定是否有重复的。
SELECT *FROM (
SELECT bizdate,indexcode ,EXCHANGE ,COUNT(*) cnt
FROM  TD_IND_INDEX  
GROUP BY bizdate,indexcode,EXCHANGE )
WHERE CNT >1

这时,发现查询出来的结果集中会有很多的数据。哎悲剧哇真的有重复的数据,郁闷
2。使用SQL语句将重复的去掉。。
delete from TD_IND_INDEX where id in (
select ID from (
select bizdate,indexcode ,EXCHANGE,count(*) as cnt,max(id) as id  
from TD_IND_INDEX group by bizdate,indexcode ,EXCHANGE 
) c where c.cnt >1 )

哇哈哈,删除了。于是在另一个表中也去执行这个SQL语句。。。发现。。
第二个表中每条重复的数据超过5行,FUCK。。谁搞的哇,然后回头看上面的SQL语句会发现,这个只是每次根据ID删除一次最大的一行。不完美。。。
3。用新的方案。
delete  from TD_IND_INDEX a
where (bizdate,indexcode ,EXCHANGE ) in 
(SELECT bizdate,indexcode ,EXCHANGE from TD_IND_INDEX group by bizdate,indexcode ,EXCHANGE having count(*) > 1)
and ID not in (select min(ID) from TD_IND_INDEX group by bizdate,indexcode ,EXCHANGE having count(*)>1)

恩,这次貌似大功告成了。。。但是。。效率不咋滴。。呵呵
获得所有单列重复的详细数据:
 select * from tuser a 
inner join 
(SELECT address, count(address) cnt from tuser  group by address)b
on a.address=b.address and b.cnt>1


你可能感兴趣的:(sql,C++,c,C#,Exchange)