Removing duplicate rows from table in Oracle

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

提交回复
热议问题