SQL query to find duplicate rows, in any table

后端 未结 4 1918
隐瞒了意图╮
隐瞒了意图╮ 2021-01-18 18:12

I\'m looking for a schema-independent query. That is, if I have a users table or a purchases table, the query should be equally capable of catchin

4条回答
  •  独厮守ぢ
    2021-01-18 18:33

    I have done this using CTEs in SQL Server.

    Here is a sample on how to delete dupes but you should be able to adapt it easily to find dupes:

    WITH CTE (COl1, Col2, DuplicateCount)
    AS
    (
        SELECT COl1,Col2,
        ROW_NUMBER() OVER(PARTITION BY COl1,Col2 ORDER BY Col1) AS DuplicateCount
        FROM DuplicateRcordTable
    )
    DELETE
    FROM CTE
    WHERE DuplicateCount > 1
    GO
    

    Here is a link to an article where I got the SQL:

    http://blog.sqlauthority.com/2009/06/23/sql-server-2005-2008-delete-duplicate-rows/

提交回复
热议问题