oracle 刪除重複數據方式

Oracle 刪除重複數據的幾種方法
去重
第一種:distinct
create table tmp_t3 as select distinct * from t3;
drop table t3;
alter table tmp_t2 rename to t3;
-- 第二種,用rowid
delete from t2
    where rowid <>( select min(rowid)
                     from t2 b
                     where b.c1 = t2.c1
                       and b.c2 = t2.c2 )
 
---第三種, 用rowid + group by 的方法
delete from T2
    where rowid not in (select min(rowid)
    from t2 group by c1,c2 );
 
delete from t2 
where not exists (select 1 from (select min(rowid) rid from t2 group by c1,c2) b where b.rid=t2.rowid)
 
---第四種, 用分析函數
delete from t2 where rowid in 
(select b.rd from 
(select rowid rd,row_number() over(partition by c1,c2 order by c1) rn 
from t2) b 
where b.rn > 1);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章