删除某些值出现多次SQL的行

时间:2012-08-24 21:19:56

标签: sql oracle

  

可能重复:
  Removing duplicate rows from table in Oracle

以下是我目前所拥有的一个例子:

ID        Name1    Name2
ABC123    Bob      Jones
ABC123    Bob      Smith
ABC123    Bob      Hammond
DEF234    Tim      Whatever

我希望上面的表格看起来像这样:

ID        Name1    Name2
ABC123    Bob      Jones
DEF234    Tim      Whatever

我想删除共享ID字段的所有行,我不关心保留或删除哪些行,只是总行数等于唯一ID的数量。

3 个答案:

答案 0 :(得分:3)

DELETE FROM YOURTABLE Y WHERE ROWID > (SELECT min(rowid) FROM YOURTABLE X
WHERE X.ID = Y.ID)

答案 1 :(得分:0)

如果您因任何原因没有全局唯一ID,那么这样的事情应该有效:

DELETE FROM Table WHERE CONCAT(ID,Name1,Name2) NOT IN ( 
       SELECT MIN(CONCAT(ID,Name1,Name2)) FROM Table GROUP BY ID)

答案 2 :(得分:0)

 DELETE FROM your_table
      WHERE ROWID IN (
               SELECT rid
                  FROM (SELECT ROWID rid,
                           ROW_NUMBER () OVER (PARTITION BY ID ORDER BY ROWID) rn
                         FROM your_table)
                WHERE rn <> 1);