Removing duplicate rows from table in Oracle

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

    I didn't see any answers that use common table expressions and window functions. This is what I find easiest to work with.

    DELETE FROM
     YourTable
    WHERE
     ROWID IN
        (WITH Duplicates
              AS (SELECT
                   ROWID RID, 
                   ROW_NUMBER() 
                   OVER(
                   PARTITION BY First_Name, Last_Name, Birth_Date)
                      AS RN
                   SUM(1)
                   OVER(
                   PARTITION BY First_Name, Last_Name, Birth_Date
                   ORDER BY ROWID ROWS BETWEEN UNBOUNDED PRECEDING 
                                           AND UNBOUNDED FOLLOWING)
                       AS CNT
                  FROM
                   YourTable
                  WHERE
                   Load_Date IS NULL)
         SELECT
          RID
         FROM
          duplicates
         WHERE
          RN > 1);
    

    Somethings to note:

    1) We are only checking for duplication on the fields in the partition clause.

    2) If you have some reason to pick one duplicate over others you can use an order by clause to make that row will have row_number() = 1

    3) You can change the number duplicate preserved by changing the final where clause to "Where RN > N" with N >= 1 (I was thinking N = 0 would delete all rows that have duplicates, but it would just delete all rows).

    4) Added the Sum partition field the CTE query which will tag each row with the number rows in the group. So to select rows with duplicates, including the first item use "WHERE cnt > 1".

    0 讨论(0)
  • 2020-11-22 13:34
    create table abcd(id number(10),name varchar2(20))
    
    insert into abcd values(1,'abc')
    
    insert into abcd values(2,'pqr')
    
    
    insert into abcd values(3,'xyz')
    
    insert into abcd values(1,'abc')
    
    insert into abcd values(2,'pqr')
    
    insert into abcd values(3,'xyz')
    
    
    select * from abcd
    id  Name
    1   abc
    2   pqr
    3   xyz
    1   abc
    2   pqr
    3   xyz
    
    Delete Duplicate record but keep Distinct Record in table 
    
    DELETE 
    FROM abcd a
    WHERE ROWID > (SELECT MIN(ROWID) FROM abcd b
    WHERE b.id=a.id
    );
    
    run the above query 3 rows delete 
    
    select * from abcd
    
    id  Name 
    1   abc
    2   pqr
    3   xyz
    
    0 讨论(0)
  • 2020-11-22 13:37

    Using rowid-

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

    Using self join-

    delete from emp e1
     where rowid not in
     (select max(rowid) from emp e2
     where e1.empno = e2.empno );
    
    0 讨论(0)
  • 2020-11-22 13:39
    DELETE FROM tablename a
          WHERE a.ROWID > ANY (SELECT b.ROWID
                                 FROM tablename b
                                WHERE a.fieldname = b.fieldname
                                  AND a.fieldname2 = b.fieldname2)
    
    0 讨论(0)
提交回复
热议问题