How to delete duplicate rows in SQL Server?

后端 未结 23 1457
长情又很酷
长情又很酷 2020-11-22 00:58

How can I delete duplicate rows where no unique row id exists?

My table is

col1  col2 col3 col4 col5 col6 col7
john  1          


        
23条回答
  •  误落风尘
    2020-11-22 01:51

    If you have the ability to add a column to the table temporarily, this was a solution that worked for me:

    ALTER TABLE dbo.DUPPEDTABLE ADD RowID INT NOT NULL IDENTITY(1,1)
    

    Then perform a DELETE using a combination of MIN and GROUP BY

    DELETE b
    FROM dbo.DUPPEDTABLE b
    WHERE b.RowID NOT IN (
                         SELECT MIN(RowID) AS RowID
                         FROM dbo.DUPPEDTABLE a WITH (NOLOCK)
                         GROUP BY a.ITEM_NUMBER,
                                  a.CHARACTERISTIC,
                                  a.INTVALUE,
                                  a.FLOATVALUE,
                                  a.STRINGVALUE
                     );
    

    Verify that the DELETE performed correctly:

    SELECT a.ITEM_NUMBER,
        a.CHARACTERISTIC,
        a.INTVALUE,
        a.FLOATVALUE,
        a.STRINGVALUE, COUNT(*)--MIN(RowID) AS RowID
    FROM dbo.DUPPEDTABLE a WITH (NOLOCK)
    GROUP BY a.ITEM_NUMBER,
        a.CHARACTERISTIC,
        a.INTVALUE,
        a.FLOATVALUE,
        a.STRINGVALUE
    ORDER BY COUNT(*) DESC 
    

    The result should have no rows with a count greater than 1. Finally, remove the rowid column:

    ALTER TABLE dbo.DUPPEDTABLE DROP COLUMN RowID;
    

提交回复
热议问题