保留表中重复记录中第一行数据,删除其他数据

时间:2020-12-01 15:03:52

CREATE TABLE #TableA
(
 Id int
 ,Name nvarchar(20)
)
GO
INSERT INTO #TableA VALUES(1,'222')
INSERT INTO #TableA VALUES(2,'2323')
INSERT INTO #TableA VALUES(3,'DSF')
INSERT INTO #TableA VALUES(4,'234')
INSERT INTO #TableA VALUES(5,'SDFSF')
INSERT INTO #TableA VALUES(6,'222')
INSERT INTO #TableA VALUES(7,'2323')
INSERT INTO #TableA VALUES(8,'2323')

select * from #TableA

delete #TableA from
#TableA,(
select  Id,ROW_NUMBER() over(partition by Name order by Id) as rn from #TableA
) nnt
WHERE #TableA.Id = nnt.Id and nnt.rn > 1


select * from #TableA

 

总结:ROW_NUMBER() partition by联用

 

还有其他方法,以前写过,大脑突然短路,等想起来再补充