2016-03-18 78 views
1

我的表中有行需要根據幾列重複進行刪除。在ORACLE SQL中刪除半重複行

e.g Col1中,col2的,COL3,COL4

如果Col1中,col2的和COL3是重複的,無論是在什麼COL4價值我想這兩個副本刪除。我該怎麼做呢?

+0

之前添加一些示例表的數據,而「刪除重複項」之後。 – jarlh

回答

2

您可以使用where條款做到這一點:通過這些ID

delete from t 
    where (col1, col2, col3) in (select col1, col2, col3 
           from t 
           group by col1, col2, col3 
           having count(*) > 1 
           ); 
2

集團與具有是否有重複檢查。用這樣找到的重複項刪除記錄。

delete from mytable 
where (col1,col2,col3) in 
(
    select col1,col2,col3 
    from mytable 
    group by col1,col2,col3 
    having count(*) > 1 
); 
1

使用EXISTS到如果與相同COL1,COL2和COL3另一行以較低的COL4值存在刪除行。即保留一個col1,col2,col3行。

delete from tablename t1 
where exists (select 1 from tablename t2 
       where t2.col1 = t1.col1 
       and t2.col2 = t1.col2 
       and t2.col3 = t1.col3 
       and t2.col4 < t1.col4) 

同時刪除/所有的行,跳過COL4條件,做一個group by代替:

delete from tablename t1 
where exists (select 1 from tablename t2 
       where t2.col1 = t1.col1 
       and t2.col2 = t1.col2 
       and t2.col3 = t1.col3 
       group by t2.col1, t2.col2, t2.col3 
       having count(*) > 1) 
+0

。 。 「我希望這兩個副本被刪除」。 –

+0

@GordonLinoff,看起來像我讀得太快......將編輯。 – jarlh