oracle中删除重复记录,有好几种方法。
第一种:把原来表中的数据都转存到一个临时表中,用distinct查询出要存到临时表去的数据。
不过这种方法只适用于表中每一列都相同的情况。
第二种:利用oracle中rowid唯一的特性。比如tableA有id,name,age三列。要删除表中name,age重复的数据时,则第一种方法就不适用了。
第三种:
第三种和第一种差不多。
第一种:把原来表中的数据都转存到一个临时表中,用distinct查询出要存到临时表去的数据。
create table temp_table as select distinct * from table
不过这种方法只适用于表中每一列都相同的情况。
第二种:利用oracle中rowid唯一的特性。比如tableA有id,name,age三列。要删除表中name,age重复的数据时,则第一种方法就不适用了。
delete from tableA t1
where t1.rowid not in
(select max(rowid) from tableA t2 group by t2.name, t2.age)
第三种:
delete from tableA
where rowid in (select row_id
from (select rowid row_id,tableA.*,
row_number() over(partition by name, age order by rowid) rn
from tableA ) where rn <>1)
第三种和第一种差不多。