Delete Duplicate SQL Records

2019-01-08 00:40发布

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

Related:

Removing duplicate records from table

2条回答
ゆ 、 Hurt°
2楼-- · 2019-01-08 01:06

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
查看更多
小情绪 Triste *
3楼-- · 2019-01-08 01:08

I got it! Simple and it worked great.

delete 
   t1 
from 
   tTable t1, tTable t2 
where 
   t1.locationName = t2.locationName and  
   t1.id > t2.id 

http://www.cryer.co.uk/brian/sql/sql_delete_duplicates.htm

查看更多
登录 后发表回答