Removing duplicate rows from table in Oracle

前端 未结 22 1509
醉话见心
醉话见心 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:28

    You should do a small pl/sql block using a cursor for loop and delete the rows you don't want to keep. For instance:

    declare
    prev_var my_table.var1%TYPE;
    
    begin
    
    for t in (select var1 from my_table order by var 1) LOOP
    
    -- if previous var equal current var, delete the row, else keep on going.
    end loop;
    
    end;
    
    0 讨论(0)
  • 2020-11-22 13:28
    DELETE from table_name where rowid not in (select min(rowid) FROM table_name group by column_name);
    

    and you can also delete duplicate records in another way

    DELETE from table_name a where rowid > (select min(rowid) FROM table_name b where a.column=b.column);
    
    0 讨论(0)
  • 2020-11-22 13:29

    Solution 4)

     delete from emp where rowid in
                (
                 select rid from
                    (
                      select rowid rid,
                      dense_rank() over(partition by empno order by rowid
                    ) rn
                 from emp
                )
     where rn > 1
    );
    
    0 讨论(0)
  • 2020-11-22 13:31

    From DevX.com:

    DELETE FROM our_table
    WHERE rowid not in
    (SELECT MIN(rowid)
    FROM our_table
    GROUP BY column1, column2, column3...) ;
    

    Where column1, column2, etc. is the key you want to use.

    0 讨论(0)
  • 2020-11-22 13:31

    To select the duplicates only the query format can be:

    SELECT GroupFunction(column1), GroupFunction(column2),..., 
    COUNT(column1), column1, column2...
    FROM our_table
    GROUP BY column1, column2, column3...
    HAVING COUNT(column1) > 1
    

    So the correct query as per other suggestion is:

    DELETE FROM tablename a
          WHERE a.ROWID > ANY (SELECT b.ROWID
                                 FROM tablename b
                                WHERE a.fieldname = b.fieldname
                                  AND a.fieldname2 = b.fieldname2
                                  AND ....so on.. to identify the duplicate rows....)
    

    This query will keep the oldest record in the database for the criteria chosen in the WHERE CLAUSE.

    Oracle Certified Associate (2008)

    0 讨论(0)
  • 2020-11-22 13:32

    Solution 1)

    delete from emp
    where rowid not in
    (select max(rowid) from emp group by empno);
    

    Solution 2)

    delete from emp where rowid in
                   (
                     select rid from
                      (
                        select rowid rid,
                          row_number() over(partition by empno order by empno) rn
                          from emp
                      )
                    where rn > 1
                   );
    

    Solution 3)

    delete from emp e1
             where rowid not in
              (select max(rowid) from emp e2
               where e1.empno = e2.empno ); 
    
    0 讨论(0)
提交回复
热议问题