我有一张看起来像这样的桌子
表格1 ID,名称
表格1
ID,名称
如何编写查询以删除所有具有重复名称的行,但保留具有较低ID的行?
如果您使用的是SQL Server 2005或更高版本:
With Dups As ( Select Id, Name , Row_Number() Over ( Partition By Name Order By Id ) As Num From Table1 ) Delete Table1 Where Id In ( Select Id From Dups Where Num > 1 )
如果使用SQL Server 2000及更低版本
Delete Table1 Where Exists ( Select 1 From Table1 As T1 Where T1.Name = Table1.Name Having Min( T1.Id ) <> Table1.Id )