Wednesday, September 18, 2013

How to Delete duplicate records in Oracle table

Many times we get duplicate records in a table. Mostly we can truncate the whole table and reload it again from source, but if the table is big and takes long time to reload, we should probably not truncate the table and reload it again. We can manually run a script to delete all the duplicates from the table and make it clean for users.

There are various approaches through which we can delete duplicate records, but my favorite is by row_id

Here is a script to perform that

DELETE FROM 
   table_name A
WHERE 
  a.rowid > 
   ANY (
     SELECT 
        B.rowid
     FROM 
        table_name B
     WHERE 
        A.col1 = B.col1
     AND 
        A.col2 = B.col2
        );

Please note that you must specify all of the columns that make the row a duplicate in the SQL where clause.
Hope this is helpful. :)

No comments:

Post a Comment