码迷,mamicode.com
首页 > 其他好文 > 详细

oracel 查询删除重复记录的几种方法

时间:2018-10-10 10:45:43      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:ble   style   person   重复记录   having   order by   group   字段   not   

建表语句
CREATE TABLE Persons
(
PersonID int,           
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

insert into Persons values(1,‘a‘,‘aa‘,‘aaa‘,‘aaaa‘);
insert into Persons values(1,‘a‘,‘aa‘,‘aaa‘,‘aaaa‘);
insert into Persons values(1,‘a‘,‘aa‘,‘aaa‘,‘aaaa‘);
insert into Persons values(1,‘a‘,‘aa‘,‘aaa‘,‘aaaa‘);
insert into Persons values(2,‘a‘,‘aa‘,‘aaa‘,‘aaaa‘);
insert into Persons values(2,‘a‘,‘aa‘,‘aaa‘,‘aaaa‘);
insert into Persons values(3,‘a‘,‘aa‘,‘aaa‘,‘aaaa‘);
insert into Persons values(3,‘a‘,‘aa‘,‘aaa‘,‘aaaa‘);
insert into Persons values(4,‘a‘,‘aa‘,‘aaa‘,‘aaaa‘);
insert into Persons values(5,‘a‘,‘aa‘,‘aaa‘,‘aaaa‘);

select * from Persons order by PersonID;
    PERSONID    LASTNAME    FIRSTNAME    ADDRESS      CITY
1    1          a        aa          aaa      aaaa
2    1          a        aa          aaa      aaaa
3    1          a        aa          aaa      aaaa
4    1          a        aa          aaa      aaaa
5    2          a        aa          aaa      aaaa
6    2          a        aa          aaa      aaaa
7    3          a        aa          aaa      aaaa
8    3          a        aa          aaa      aaaa
9    4          a        aa          aaa      aaaa
10    5          a        aa          aaa      aaaa


1、查找表中多余的重复记录,重复记录是根据单个字段(PersonID)来判断。
select * from Persons where PersonID in ( select PersonID from Persons group by PersonID having(count(PersonID))>1);
order by PersonID;

PERSONID    LASTNAME    FIRSTNAME    ADDRESS    CITY
1    a    aa    aaa    aaaa
1    a    aa    aaa    aaaa
1    a    aa    aaa    aaaa
1    a    aa    aaa    aaaa
2    a    aa    aaa    aaaa
2    a    aa    aaa    aaaa
3    a    aa    aaa    aaaa
3    a    aa    aaa    aaaa


2、删除表中多余的重复记录,重复记录是根据单个字段(PersonID)来判断,只留有rowid最小的记录。
select * from Persons
where PersonID  in (select  PersonID  from Persons  group  by  PersonID   having  count(PersonID) > 1)
and rowid not in (select min(rowid) from  Persons  group by PersonID  having count(PersonID )>1)
order by PersonID;

delete from Persons
where PersonID  in (select  PersonID  from Persons  group  by  PersonID   having  count(PersonID) > 1)
and rowid not in (select min(rowid) from  Persons  group by PersonID  having count(PersonID )>1)

PERSONID    LASTNAME    FIRSTNAME    ADDRESS    CITY
1    a    aa    aaa    aaaa
1    a    aa    aaa    aaaa
1    a    aa    aaa    aaaa
2    a    aa    aaa    aaaa
3    a    aa    aaa    aaaa


3、查找表中多余的重复记录(多个字段)
select * from Persons a
where (a.PersonID,a.City) in  (select PersonID,City from Persons group by PersonID,City  having count(*) > 1)
order by PersonID;

PERSONID    LASTNAME    FIRSTNAME    ADDRESS    CITY
1    a    aa    aaa    aaaa
1    a    aa    aaa    aaaa
1    a    aa    aaa    aaaa
1    a    aa    aaa    aaaa
2    a    aa    aaa    aaaa
2    a    aa    aaa    aaaa
3    a    aa    aaa    aaaa
3    a    aa    aaa    aaaa


4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
select * from Persons a
where (a.PersonID,a.City) in  (select PersonID,City from Persons group by PersonID,City having count(*) > 1)
and rowid not in (select min(rowid) from Persons group by PersonID,City having count(*)>1)
order by PersonID;

delete from Persons a
where (a.PersonID,a.City) in  (select PersonID,City from Persons group by PersonID,City having count(*) > 1)
and rowid not in (select min(rowid) from Persons group by PersonID,City having count(*)>1)

PERSONID    LASTNAME    FIRSTNAME    ADDRESS    CITY
1    a    aa    aaa    aaaa
1    a    aa    aaa    aaaa
1    a    aa    aaa    aaaa
2    a    aa    aaa    aaaa
3    a    aa    aaa    aaaa


5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select * from Persons a
where (a.PersonID,a.City) in  (select PersonID,City from Persons group by PersonID,City having count(*) > 1)
and rowid not in (select min(rowid) from Persons group by PersonID,City having count(*)>1)
and rowid not in (select min(rowid) from Persons group by PersonID,seq having count(*)>1)
order by PersonID;

PERSONID    LASTNAME    FIRSTNAME    ADDRESS    CITY
1    a    aa    aaa    aaaa
1    a    aa    aaa    aaaa
1    a    aa    aaa    aaaa
2    a    aa    aaa    aaaa
3    a    aa    aaa    aaaa


oracel 查询删除重复记录的几种方法

标签:ble   style   person   重复记录   having   order by   group   字段   not   

原文地址:https://www.cnblogs.com/sunziying/p/9764622.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!