Removing duplicate rows from table in Oracle

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

    solution :

    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
    );
    
    0 讨论(0)
  • 2020-11-22 13:22
    create or replace procedure delete_duplicate_enq as
        cursor c1 is
        select *
        from enquiry;
    begin
        for z in c1 loop
            delete enquiry
            where enquiry.enquiryno = z.enquiryno
            and rowid > any
            (select rowid
            from enquiry
            where enquiry.enquiryno = z.enquiryno);
        end loop;
     end delete_duplicate_enq;
    
    0 讨论(0)
  • 2020-11-22 13:23

    Use the rowid pseudocolumn.

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

    Where column1, column2, and column3 make up the identifying key for each record. You might list all your columns.

    0 讨论(0)
  • 2020-11-22 13:23
    DELETE FROM tableName  WHERE ROWID NOT IN (SELECT   MIN (ROWID) FROM table GROUP BY columnname);
    
    0 讨论(0)
  • 2020-11-22 13:23
    delete from dept
    where rowid in (
         select rowid
         from dept
         minus
         select max(rowid)
         from dept
         group by DEPTNO, DNAME, LOC
    );
    
    0 讨论(0)
  • 2020-11-22 13:25

    From Ask Tom

    delete from t
     where rowid IN ( select rid
                        from (select rowid rid, 
                                     row_number() over (partition by 
                             companyid, agentid, class , status, terminationdate
                                       order by rowid) rn
                                from t)
                       where rn <> 1);
    

    (fixed the missing parenthesis)

    0 讨论(0)
提交回复
热议问题