oracle删除id重复数据,仅保留一条

由于id未设置主键,导致重复导入数据时,id有很多重复,想要每个id只保留一条数据。肯定不能使用max(id)或min(id)来执行了。

但是由于rowID是唯一的,所以还是很好解决。

以下提供两种办法:
1.先根据id分组排序,删掉rowID等于排序后顺序不等于1的数据


delete from tablename where rowid in (select a.rowid from (select id,row_number() over(partition by id order by id) as qrowid from tablename ) a where a.qrowid != 1) 

2.删除id条数大于1的且rowID最大的数据

delete from tablename t
 where t.id in (select id from tablename group by id having count(1) > 1)
   and t.rowid not in
       (select max(rowid) from tablename group by id having count(1) > 1);

你可能感兴趣的:(Oracle,去重,id重复)