2016-03-18 80 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