Oracle - deleting duplicates

风格不统一 提交于 2021-02-05 11:36:23

问题


I have found the following way for removing duplicates:

DELETE FROM
   table_name A
WHERE
  a.rowid >
   ANY (
     SELECT
        B.rowid
     FROM
        table_name B
     WHERE
        A.col1 = B.col1
     AND
        A.col2 = B.col2
        ); 

Could someone explain me step by step how does the query works?

TIA!


回答1:


In Oracle, ROWID is a pseudo column points to the physical location of a row. The query does a self join and fetches those rows which have the same value of column 1 & column 2 - with the assumption that these keys are enough to identify as duplicate row.

Once the rows are fetched, the query then deletes those rowids which are larger than the first row fetched, thereby deleting duplicates




回答2:


Like this:

DELETE FROM         // The command to delete
   table_name A     //the table in which you want to remove duplicate
WHERE               //condition
  a.rowid >         //checking the rowid which oracle adds to each row. Oracle Database rowid values contain information necessary to locate a row.
   ANY (             //any row which has a condition
     SELECT          //select
        B.rowid      //rowid
     FROM             //from  
        table_name B    //table name with alias name as B. Is used for making a self join
     WHERE               //condition
        A.col1 = B.col1    //where the column1 has the same rowid 
     AND                    //and
        A.col2 = B.col2     //where the column2 has the same rowid 
        ); 


来源:https://stackoverflow.com/questions/33801875/oracle-deleting-duplicates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!