Search code examples
sqloraclesql-delete

Oracle : How to delete duplicates rows when no distinct value exists?


We have a table with many rows duplicated (2 to 4 times) (each row has the same value for each column). This table has 4 columns but no primary key.

So how to delete the duplicated rows ?

Instance  status  updatetime              name
-----------------------------------------------
gdt546    4       2016/02/13 10:13:18     basic
gdt546    4       2016/02/13 10:13:18     basic
ort87a    1       2016/02/16 19:09:43     High
ort87a    1       2016/02/16 19:09:43     High
ort87a    1       2016/02/16 19:09:43     High

Edited:

The output should be:

 Instance  status  updatetime              name
-----------------------------------------------
gdt546    4       2016/02/13 10:13:18     basic
ort87a    1       2016/02/16 19:09:43     High

Solution

  • Assuming the name of the table is TABLE_NAME and the intent is to delete the duplicate rows such that it retains only one of them, please find the below query:

    DELETE FROM table_name A 
    WHERE  A.ROWID > ANY (SELECT B.ROWID 
                          FROM   table_name B 
                          WHERE  A.INSTANCE = B.INSTANCE 
                                 AND A.status = B.status 
                                 AND A.updatetime = B.updatetime 
                                 AND A.name = B.name);