适合初学者阅读
引:
我们在做数据库开发用 pl sql 加工数据时,经常会遇到需要去除重复脏数据的情况,所以特地对此做个简单的总结,以便以后用到回顾。
重复数据分为 整条记录重复 和某个字段重复,删除目标分为重复数据全删除和重复数据删除留一条。
解决:
1 distinct 删除某字段所有重复的数据
2 删除 某个字段重复数据 但是留下最小rowid的那条(即还留一条 且不重复)
delete from wh_td2 a
where a.rowid > (select min(b.rowid)
from wh_td2 b
where a.processinstid = b.processinstid)
3 group by 删除某字段所有重复的数据
delete from wh_td2 a
where a.processinstid > (select b.processinstid
from wh_td2 b
group by b.processinstid having count(1)>1)
4 row_number over() 删除某字段所有重复的数据
select t.object_id,
row_number() over(partition by t.object_id order by t.object_id) del_flag
from scott.dba_objects_bak t;
delete from scott.dba_objects_bak
where object_id in (select object_id
from (select t.object_id,
row_number() over(partition by t.object_id order by t.object_id) as del_flag
from scott.dba_objects_bak t)where del_flag > 1 );