1、查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断
select * from 表 where Id in (select Id from 表 group byId having count(Id) > 1)
2、删除表中多余的重复记录,重复记录是根据单个字段(Id)来判断,只留有rowid最小的记录
DELETE from 表 WHERE (id) IN ( SELECT id FROM 表 GROUP BY id HAVING COUNT(id) > 1) AND ROWID NOT IN (SELECT MIN(ROWID) FROM 表 GROUP BY id HAVING COUNT(*) > 1);
3、查找表中多余的重复记录(多个字段)
select * from 表 a where (a.Id,a.seq) in(select Id,seq from 表 group by Id,seq having count(*) > 1)
4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete from 表 a where (a.Id,a.seq) in (select Id,seq from 表 group by Id,seq having count(*) > 1) and rowid not in (select min(rowid) from 表 group by Id,seq having count(*)>1)
5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select * from 表 a where (a.Id,a.seq) in (select Id,seq from 表 group by Id,seq having count(*) > 1) and rowid not in (select min(rowid) from 表 group by Id,seq having count(*)>1)
上述转载自:http://www.cnblogs.com/252e/archive/2012/09/13/2682817.html
实际项目遇见千万级别的数据时,删除重复的数据并不适用(在oracle上800多万条数据用伤处方法去重复N个小时没有结果)。
下面给出在大数据情况下的去重复操作:
- 建临时表:CREATE TABLE tt as select a.name, MAX(a.ROWID) dataid from tablenamea GROUP BY a.name; 或 CREATE TABLE tt AS (select distinct * from tablename);
- 清空原表数据:truncatetable tablename;
- 将临时表数据填充到原表中:insert into tablename (select * from tt); --千万级别数据2分钟左右完成
- 删除临时表:drop table tt;
- 提交:commit