How to delete duplicate records from a table in oracle

故事扮演 提交于 2019-12-13 10:52:08

问题


select * from ap;

select name from ap group by name having count(*)>1;

I want to delete duplicates records from this table.


回答1:


delete from table_name a
where 
a.rowid > any (select b.rowid from table_name b where a.col1 = b.col1 and a.col2 = b.col2);



回答2:


If you want to keep one record for each name:

delete from ap
    where ap.id > (select min(ap2.id) from ap ap2 where ap2.name = ap.name)



回答3:


1. solution

delete from emp
    where rowid not in
    (select max(rowid) from emp group by empno);

2. sloution

delete from emp where rowid in
               (
                 select rid from
                  (
                    select rowid rid,
                      row_number() over(partition by empno order by empno) rn
                      from emp
                  )
                where rn > 1
               );



回答4:


Try this ; inner query it will return all max rowid based on group by clause that is name here so only one rowid for all duplicate records. delete will keep only those rows and delete all other which is duplicate.

delete from ap where rowid not in (select max(rowid) from ap group by name); 



回答5:


Delete from ap where ap.rowid not in ( select min (rowid) from ap a group by a.name);

This will remove duplicates, triplicate so on. It will keep one occurerrence for one record. Min (rowid) or max (rowid) you have to choose either you want to store the first/oldest inserted record or latest/last inserted record.



来源:https://stackoverflow.com/questions/31315856/how-to-delete-duplicate-records-from-a-table-in-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!