Search code examples
oracle-databaseplsqlduplicatesrowid

Delete duplicate rows - Oracle


Here is an example of some of my data. I am trying to remove all extra data where the QueryID and RoyID are the same as other entries in the table, however, I would like to keep atleast one record. I'm not sure I'll be able to use the min or max functions on the ID column as some of the records have IDs like DEMO_12345 etc.

ID          QUERY_ID ROYALTY_ID
RTSQR1652   SQ1421  ROY25644005
RTSQR1653   SQ1421  ROY25636406
RTSQR1654   SQ1421  ROY25636557
RTSQR1655   SQ1421  ROY25636558
RTSQR1656   SQ1421  ROY25636559
RTSQR1657   SQ1421  ROY25636560

I was thinking to use ROWID instead of ID. Would the following query work?

 DELETE FROM RT_SOURCE_QUERY_ROYALTIES WHERE ROWID NOT IN (
 SELECT MAX(ROWID) FROM RT_SOURCE_QUERY_ROYALTIES GROUP BY ROYALTY_ID, QUERY_ID);

Solution

  • Just run a test

    drop table test1;
    
    create table test1 (a number,b number, c number);
    
    insert into test1 values (1,1,2);
    insert into test1 values (1,1,3);
    insert into test1 values (1,2,2);
    insert into test1 values (2,1,2);
    insert into test1 values (2,2,2);
    insert into test1 values (1,1,2);
    
    select * from test1;
    
    delete from test1 where rowid not in (select max(rowid) from test1 group by a,b);
    
    select * from test1;
    

    Works as expected, doesn't it ...

    table TEST1 dropped.
    table TEST1 created.
    1 rows inserted.
    1 rows inserted.
    1 rows inserted.
    1 rows inserted.
    1 rows inserted.
    1 rows inserted.
    A B C
    - - -
    1 1 2 
    1 1 3 
    1 2 2 
    2 1 2 
    2 2 2 
    1 1 2 
    
     6 rows selected 
    
    2 rows deleted.
    A B C
    - - -
    1 2 2 
    2 1 2 
    2 2 2 
    1 1 2