oracle如何删除table中的重复记录

How to delete duplicate records from a table in oracle

select * from ap;

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

我想删除此 table 中的重复记录。

如果您想为每个名字保留一条记录:

delete from ap
    where ap.id > (select min(ap2.id) from ap ap2 where ap2.name = ap.name)
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);

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

这将删除重复项,一式三份等等。它将为一个记录保留一个事件。 Min (rowid)max (rowid) 您必须选择要存储 first/oldest 插入的记录或 latest/last 插入的记录。

试试这个; 内部查询它将 return all max rowid based on group by clause which is name here 因此所有重复记录只有一个 rowid。删除将只保留那些行并删除所有其他重复的行。

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

1.解决方案

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