Removing duplicate rows from table in Oracle

前端 未结 22 1778
醉话见心
醉话见心 2020-11-22 12:57

I\'m testing something in Oracle and populated a table with some sample data, but in the process I accidentally loaded duplicate records, so now I can\'t create a primary ke

22条回答
  •  自闭症患者
    2020-11-22 13:17

    Check below scripts -

    1.

    Create table test(id int,sal int); 
    

    2.

        insert into test values(1,100);    
        insert into test values(1,100);    
        insert into test values(2,200);    
        insert into test values(2,200);    
        insert into test values(3,300);    
        insert into test values(3,300);    
        commit;
    

    3.

     select * from test;    
    

    You will see here 6-records.
    4.run below query -

    delete from 
       test
    where rowid in
     (select rowid from 
       (select 
         rowid,
         row_number()
        over 
         (partition by id order by sal) dup
        from test)
      where dup > 1)
    
    1. select * from test;

    You will see that duplicate records have been deleted.
    Hope this solves your query. Thanks :)

提交回复
热议问题