删除oracle中的重复列

时间:2012-07-20 12:58:39

标签: oracle oracle11g

我有一个包含4列和数千条记录的表。我想根据三列(比如col1,col2,col3)删除重复的行。 我必须根据

等标准删除行
1) found a row which has col1 and col2 equal and
2) in those duplicate record i have to delete those rows on which col3 is null- keep  one row
  (if no other record with combination of col1 and col2 doesn't have not null col3) 

col1 col2 col3
---- ---- ----
1    1    null
1    1    null
1    2    null
1    2    1
1    2    1
1    2    null
1    2    2
1    2    3

在这个例子中我要删除1或2. 3,4,5,6有重复的列,其中一些有第三列不为null我想只保留一行第三列不为null (如果第三列也相等,只保留一行)

2 个答案:

答案 0 :(得分:2)

假设您的数据如下所示:

col1 col2 col3
---- ---- ----
1    1    null
1    1    null
1    2    null
1    2    1
1    2    2
1    2    3
1    2    null

是否可以删除前两行,因为col1col2匹配,col3两者都为空?如果没有,请点击这里我的sql:

DELETE FROM table_name A
WHERE  exists (
  SELECT 1
  FROM table_name B
  WHERE A.col1 = B.col1
  AND   A.col2 = B.col2
  AND   B.col3 is not null)
AND A.col3 is null

答案 1 :(得分:0)

DELETE FROM table_name A WHERE ROWID > (SELECT min(rowid) FROM table_name B
WHERE A.clmn1 = B.clmn1 AND A.clmn2 = B.clmn2 AND A.clmn3 is NULL AND b.clmn3 is NULL);

检查一下.. 评论是否有任何错误。