sql skills

>>>Oracle queries duplicate data and deletes it, keeping only one record

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)

 

6. After oracle grouping, take the first data of each group

 

SELECT *       
    FROM (SELECT ROW_NUMBER() OVER(PARTITION BY x ORDER BY y DESC) rn,       
          test1.*       
          FROM test1)       
   WHERE rn = 1  ;

Guess you like

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