適合初學者閱讀
引:
我們在做數據庫開發用 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 );