Search code examples
oracle-databaseduplicatesdelete-roworacle8i

How to clear table in oracle


I have data table in Oracle 8,1. There are about a million rows. But lots of rows duplicates by the same columns. I need to know fastest way to clear this data. For example I have:

id name surname date
21 'john' 'smith' '2012 12 12'; 
21 'john' 'smith' '2012 12 13';
21 'john' 'smith' '2012 12 14';
....

And now I need to delete first two rows as they duplicates by first three columns and keep the row with the latest date.


Solution

  • If there are really lots of duplicates, I'd recommend to recreate the table with only the clean data:

    CREATE TABLE tmp AS 
    SELECT id, name, surname, max(d) as d
       FROM t
      GROUP BY id, name, surname;
    

    and then replace the original table with the original table:

    RENAME your_table TO old_table;
    RENAME tmp_table TO your_table;
    

    Don't forget to move indexes, constraints and privileges...