Oracle 查询并删除重复记录的SQL语句,如下内容
做项目的时候,一位同事导数据的时候,不小心把一个表中的数据全都搞重了,也就是说,这个表里所有的记录都有一条重复的。这个表的数据是千万级的,而且是生产系统。也就是说,不能把...
做项目的时候,一位同事导数据的时候,不小心把一个表中的数据全都搞重了,
也就是说,这个表里所有的记录都有一条重复的。这个表的数据是千万级的,
而且是生产系统。也就是说,不能把所有的记录都删除,而且必须快速的把重复记录删掉。
对此,总结了一下删除重复记录的方法,以及每种方法的优缺点。
为了陈诉方便,假设表名为Tbl,表中有三列col1,col2,col3,其中col1 是主键 展开
也就是说,这个表里所有的记录都有一条重复的。这个表的数据是千万级的,
而且是生产系统。也就是说,不能把所有的记录都删除,而且必须快速的把重复记录删掉。
对此,总结了一下删除重复记录的方法,以及每种方法的优缺点。
为了陈诉方便,假设表名为Tbl,表中有三列col1,col2,col3,其中col1 是主键 展开
2个回答
2015-03-24
展开全部
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select * from people
where peopleId in (select peopleId from people group by peopleId
having count(peopleId) > 1)
2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有 rowid最小的记录
delete from people
where peopleId in (select peopleId from people group by peopleId
having count(peopleId) > 1)
and rowid not in (select min(rowid) from people group by peopleId having
count(peopleId )>1)
注:rowid为oracle自带不用该.....
3、查找表中多余的重复记录(多个字段)
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by
peopleId,seq having count(*) > 1)
4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by
peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having
count(*)>1)
5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by
peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having
count(*)>1)
select * from people
where peopleId in (select peopleId from people group by peopleId
having count(peopleId) > 1)
2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有 rowid最小的记录
delete from people
where peopleId in (select peopleId from people group by peopleId
having count(peopleId) > 1)
and rowid not in (select min(rowid) from people group by peopleId having
count(peopleId )>1)
注:rowid为oracle自带不用该.....
3、查找表中多余的重复记录(多个字段)
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by
peopleId,seq having count(*) > 1)
4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by
peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having
count(*)>1)
5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by
peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having
count(*)>1)
展开全部
病句:表中有重复 col1是主键 哈哈.....ora 00001
给个例子 根据ROWID来删 效率应该还可以 自己试试
SQL> select a.*,rowid from t a ;
ID SEX NAME ROWID
---------- ---- ---------- ------------------
1 clark AAAXVZAAEAAD8WAAAA
2 johns AAAXVZAAEAAD8WAAAB
3 james AAAXVZAAEAAD8WAAAC
1 clark AAAXVZAAEAAD8WAAAD
2 johns AAAXVZAAEAAD8WAAAE
3 james AAAXVZAAEAAD8WAAAF
已选择6行。
SQL> delete from t a where a.rowid not in (select min(rowid) from t group by id,sex,name);
已删除3行。
SQL> commit;
提交完成。
SQL> select a.*,rowid from t a ;
ID SEX NAME ROWID
---------- ---- ---------- ------------------
1 clark AAAXVZAAEAAD8WAAAA
2 johns AAAXVZAAEAAD8WAAAB
3 james AAAXVZAAEAAD8WAAAC
给个例子 根据ROWID来删 效率应该还可以 自己试试
SQL> select a.*,rowid from t a ;
ID SEX NAME ROWID
---------- ---- ---------- ------------------
1 clark AAAXVZAAEAAD8WAAAA
2 johns AAAXVZAAEAAD8WAAAB
3 james AAAXVZAAEAAD8WAAAC
1 clark AAAXVZAAEAAD8WAAAD
2 johns AAAXVZAAEAAD8WAAAE
3 james AAAXVZAAEAAD8WAAAF
已选择6行。
SQL> delete from t a where a.rowid not in (select min(rowid) from t group by id,sex,name);
已删除3行。
SQL> commit;
提交完成。
SQL> select a.*,rowid from t a ;
ID SEX NAME ROWID
---------- ---- ---------- ------------------
1 clark AAAXVZAAEAAD8WAAAA
2 johns AAAXVZAAEAAD8WAAAB
3 james AAAXVZAAEAAD8WAAAC
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询