SQL: Deleting duplicate records in SQL Server

前端 未结 2 1425
攒了一身酷
攒了一身酷 2021-02-06 10:35

I have an sql server database, that I pre-loaded with a ton of rows of data.

Unfortunately, there is no primary key in the database, and there is now duplicate informati

相关标签:
2条回答
  • 2021-02-06 11:10

    Well, this is one reason why you should have a primary key on the table. What version of SQL Server? For SQL Server 2005 and above:

    ;WITH r AS
    (
        SELECT col1, col2, col3, -- whatever columns make a "unique" row
        rn = ROW_NUMBER() OVER (PARTITION BY col1, col2, col3 ORDER BY col1)
        FROM dbo.SomeTable
    )
    DELETE r WHERE rn > 1;
    

    Then, so you don't have to do this again tomorrow, and the next day, and the day after that, declare a primary key on the table.

    0 讨论(0)
  • 2021-02-06 11:14

    Let's say your table is unique by COL1 and COL2.
    Here is a way to do it:

    SELECT *
    FROM (SELECT COL1, COL2, ROW_NUMBER() OVER (PARTITION BY COL1, COL2 ORDER BY COL1, COL2 ASC) AS ROWID
          FROM TABLE_NAME )T
    WHERE T.ROWID > 1
    

    The ROWID > 1 will enable you to select only the duplicated rows.

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