oracle 查詢重複數據以及刪除重複數據


<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>


查詢及刪除重複記錄的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)








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章