【发布时间】:2009-07-24 08:20:58
【问题描述】:
我想删除基于它们在某一列中具有相同值的重复记录,并在下面的示例中保留基于 InsertedDate 被认为是最新的记录。我想要一个不使用游标但基于设置的解决方案。目标:删除所有重复并保持最新。
下面的 ddl 创建了一些重复项。需要删除的记录有:John1 & John2,因为它们的ID与John3相同,而John3是最新的记录。
还需要删除记录 John5,因为还有另一条 ID = 3 且更新的记录 (John6)。
Create table dbo.TestTable (ID int, InsertedDate DateTime, Name varchar(50))
Insert into dbo.TestTable Select 1, '07/01/2009', 'John1'
Insert into dbo.TestTable Select 1, '07/02/2009', 'John2'
Insert into dbo.TestTable Select 1, '07/03/2009', 'John3'
Insert into dbo.TestTable Select 2, '07/03/2009', 'John4'
Insert into dbo.TestTable Select 3, '07/05/2009', 'John5'
Insert into dbo.TestTable Select 3, '07/06/2009', 'John6'
【问题讨论】:
标签: sql sql-server