Delete Duplicate SQL Records

前端 未结 2 617
别跟我提以往
别跟我提以往 2020-11-30 13:13

What is the simplest way to delete records with duplicate name in a table? The answers I came across are very confusing.

Related:

Removing dupl

2条回答
  •  佛祖请我去吃肉
    2020-11-30 14:03

    SQL Server 2005:

    with FirstKey
    AS
    (
        SELECT MIN(ID), Name, COUNT(*) AS Cnt
          FROM YourTable
         GROUP BY Name
         HAVING COUNT(*) > 1
    )
    DELETE YourTable
      FROM YourTable YT
      JOIN FirstKey FK ON FK.Name = YT.Name AND FK.ID != YT.ID
    

提交回复
热议问题