oracle 刪除重複數據你想不到的方法大全-開發系列(六)

適合初學者閱讀微笑


引:

我們在做數據庫開發用 pl sql 加工數據時,經常會遇到需要去除重複髒數據的情況,所以特地對此做個簡單的總結,以便以後用到回顧。

重複數據分爲 整條記錄重複 和某個字段重複,刪除目標分爲重複數據全刪除和重複數據刪除留一條


解決:


1 distinct  刪除某字段所有重複的數據


假設要去重的字段爲b 表爲 a 先建個臨時表a_temp,把select distinct a.b 的數據放入到a_temp,然後用delete from a where b in (select b from a_temp)


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() 刪除某字段所有重複的數據


下面的語句是查詢出 object_id 重複的語句,partition 分組 跟group by 分組的區別是 partition分組後 可以看到組內成員的信息,而group by 
只能看到組的總統計信息。

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 );






row_number over() 刪除某字段所有重複的數據(根據條件指定留一條)


如下圖:根據 partition 分組後的object_id 若有重複的話 ,根據object_name 排序,同樣的object_id 可以識別出object_name最小的那條數據,即按指定條件 去重了 到時候加上del_flag=1 條件即可



需要數據的話 把del_flag=1 條件後的數據 放入到新建表即可。

發佈了51 篇原創文章 · 獲贊 99 · 訪問量 54萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章