SQL删除重复记录的N种方法

上一篇 / 下一篇  2011-07-15 10:24:38 / 个人分类:SQL

查询及删除重复记录的SQL语句
查询及删除重复记录的SQL语句
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)
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)
(二)
比方说
在A表中存在一个字段“name”,
而且不同记录之间的“name”值有可能会相同,
现在就是需要查询出在该表中的各记录之间,“name”值存在重复的项;
Select Name,Count(*) From A Group By Name Having Count(*) > 1
如果还查性别也相同大则如下:
Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1

 

(1)通过建立临时表来实现

SQL>create table temp_emp as (select distinct * from employee)

SQL> truncate table employee; (清空employee表的数据)

SQL> insert into employee select * from temp_emp; (再将临时表里的内容插回来)

 

( 2)通过唯一rowid实现删除重复记录.在Oracle中,每一条记录都有一个rowid,rowid在整个数据库中是唯一的,rowid确定了每条记 录是在Oracle中的哪一个数据文件、块、行上。在重复的记录中,可能所有列的内容都相同,但rowid不会相同,所以只要确定出重复记录中那些具有最 大或最小rowid的就可以了,其余全部删除。

SQL>delete from employee e2 where rowid not in (

        select max(e1.rowid) from employee e1 where

        e1.emp_id=e2.emp_id and e1.emp_name=e2.emp_name and e1.salary=e2.salary);--这里用min(rowid)也可以。


SQL>delete from employee e2 where rowid <(

        select max(e1.rowid) from employee e1 where
        e1.emp_id=e2.emp_id and e1.emp_name=e2.emp_name and

                  e1.salary=e2.salary);


(3)也是通过rowid,但效率更高。

SQL>delete from employee where rowid not in (

        select max(t1.rowid) from employee t1 group by

         t1.emp_id,t1.emp_name,t1.salary);--这里用min(rowid)也可以。


TAG:

 

评分:0

我来说两句

我的栏目

日历

« 2024-05-05  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 5671
  • 日志数: 10
  • 建立时间: 2008-01-11
  • 更新时间: 2011-07-15

RSS订阅

Open Toolbar