Home  >  Article  >  Database  >  Oracle deletes duplicate data and keeps the first record

Oracle deletes duplicate data and keeps the first record

angryTom
angryTomOriginal
2020-02-29 10:37:468402browse

Oracle deletes duplicate data and keeps the first record

oracle deletes duplicate data and retains the first record

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

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 based on a single field (Id), leaving only the record with the smallest 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. Search Redundant duplicate records (multiple fields) in the table

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. Search The redundant duplicate records (multiple fields) in the table do not include 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)

PHP Chinese website, there are a large number of free SQL tutorials, everyone is welcome to learn!

The above is the detailed content of Oracle deletes duplicate data and keeps the first record. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Usage of Case When in SQLNext article:Usage of Case When in SQL