Oracle删除重复行并保留一行

delete from table
where id in (select id 
             from table  
             group by id 
             having count(*) >1)  
     and rowid not in 
         (select min(rowid) 
         from table 
         group by id 
         having count(*)>1);

以上代码将id重复的行删除只保留一行,先将所有重复id的行选中当做集合A,再选中id重复的行中rowid最小的行当做集合B,之后要删除的内容即A和非B的与。
以上代码常用于给某列加主键但此列有重复数据的列。

posted @ 2023-05-23 12:05  epiphanyy  阅读(120)  评论(0)    收藏  举报  来源