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

建表语句
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

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

时间: 2024-10-12 07:25:09

oracel 查询删除重复记录的几种方法的相关文章

oracle中查找和删除重复记录的几种方法总结

平时工作中可能会遇到当试图对库表中的某一列或几列创建唯一索引时,系统提示 ORA-01452 :不能创建唯一索引,发现重复记录. 下面总结一下几种查找和删除重复记录的方法(以表CZ为例): 表CZ的结构如下: 1 SQL> desc cz 2 Name Null? Type 3 ------------------------------------------------------------------- 4 C1 NUMBER(10) 5 C10 NUMBER(5) 6 C20 VARC

oracle__删除重复记录__三种方法及总结(转载百度文库)

http://wenku.baidu.com/link?url=RIENeGUK4sjxe21_RBYLYHR9tbUUCmOZQRR0mIjldXLYwRAt4khDtLQD9dFyd3rz3s_HWLvG2oErTw8sJUb1R2QLQqSZaBO3xLA8tu2qd9q --方法1:rowid --显示重复的行 select * from persons p1 where rowid<> (select max(rowid) from persons p2 where p1.pid=p

spring+hibernate删除单条记录的几种方法

spring+hibernate删除单条记录的几种方法

ORACLE查询删除重复记录三种方法

本文列举了3种删除重复记录的方法,分别是rowid.group by和distinct,小伙伴们可以参考一下. 比如现在有一人员表 (表名:peosons) 若想将姓名.身份证号.住址这三个字段完全相同的记录查询出来 代码如下: select p1.*   from persons  p1,persons  p2   where p1.id<>p2.id   and  p1.cardid = p2.cardid and p1.pname = p2.pname and p1.address =

MySQL查询删除重复记录

查询及删除重复记录的方法 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 wher

SQL Server 数据库查找重复记录的几种方法

http://www.hanyu123.cn/html/c61/6790.html 一.查某一列(或多列)的重复值.(只可以查出重复记录的值,不能查出整个记录的信息) 例如:查找stuid,stuname重复的记录: select stuid,stuname from stuinfo group by stuid,stuname having(count(*))>1 二.查某一列有重复值的记录.(此方法查出的是所有重复的记录,如果有两条记录重复的,就查出两条) 例如:查找stuid重复的记录:

ORACLE查询并删除重复记录

查询及删除重复记录的SQL语句 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select   peopleId from   people group by   peopleId having count(peopleId) > 1) 2.删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录delete from people where

Oracle 查询并删除重复记录的SQL语句

查询及删除重复记录的SQL语句 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select   peopleId from   people group by   peopleId having count(peopleId) > 1) 2.删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录delete from people where

MySQL中查询、删除重复记录的方法大全

前言 本文主要给大家介绍了关于MySQL中查询.删除重复记录的方法,分享出来供大家参考学习,下面来看看详细的介绍: 查找所有重复标题的记录: ? 1 select title,count(*) as count from user_table group by title having count>1; ? 1 SELECT * FROM t_info a WHERE ((SELECT COUNT(*) FROM t_info WHERE Title = a.Title) > 1) ORDER