Notes: Oracle queries duplicate data and deletes, keeping only one record

Reprinted from: http://www.uncletoo.com/html/oracle/1101.html

1. Find redundant duplicate records in the table. Duplicate records are judged based on a single field (Id).

select * from 表 where Id in (select Id from 表 group byId having count(Id) > 1)

 

2. Delete redundant duplicate records in the table. Duplicate records are judged according to a single field (Id), and only the record with the smallest rowid is left.

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. Find redundant duplicate records in the table (multiple fields)

select * from 表 a where (a.Id,a.seq) in(select Id,seq from 表 group by Id,seq having count(*) > 1)

 

4. Delete redundant duplicate records (multiple fields) in the table, leaving only the record with the smallest 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. Find redundant duplicate records (multiple fields) in the table, excluding the record with the smallest 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)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327014697&siteId=291194637