您的位置:首页 > 数据库 > Oracle

oracle 查询重复数据以及删除重复数据

2015-07-27 15:14 633 查看
<span style="font-family:Courier New;font-size:18px;">select a.facilityid,
a.faulttime,
a.domainid,
a.facilitytype1,
a.facilitytype2,
a.facilitytype3,
count(*)
from ips_fault_facility a
group by a.facilityid,
a.faulttime,
a.domainid,
a.facilitytype1,
a.facilitytype2,
a.facilitytype3 having count(*) > 1</span>


<span style="font-family:Courier New;font-size:18px;">create table ips_fault_facility1
as
select *
from ips_fault_facility a
where rowid <> (select min(rowid)
from ips_fault_facility b
where a.facilityid = b.facilityid
and a.faulttime = b.faulttime
and a.domainid = b.domainid
and a.facilitytype1 = b.facilitytype1
and a.status = b.status
and a.facilitytype2 = b.facilitytype2
and a.facilitytype3 = b.facilitytype3)</span>


--删除重复数据

<span style="font-family:Courier New;font-size:18px;">delete from ips_fault_facility a where exists(select 1 from ips_fault_facility1 b where a.uuid = b.uuid)</span>


Oracle
删除重复数据只留一条

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

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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  oracle 数据库