--创建临时表并向临时表中插入测试表Tmp中数据以及添加自增id:autoID
select identity(int,1,1) as autoID, * into #Tmp from test
select * from #Tmp
--根据autoID删除临时表#tmp中的重复数据,只保留每组重复数据中的第一条
DELETE FROM #Tmp WHERE autoID IN(
SELECT autoID FROM
(
SELECT autoID,字段A
,字段B,OrderNo=ROW_NUMBER() OVER(PARTITION BY 字段A
,字段B ORDER BY GETDATE())
FROM #Tmp
) Tmp
WHERE OrderNo>1
);
--清除testtab表中的所有数据
delete testtab;
--向testtab表中插入#Tmp表中被处理过的数据
insert into testtab select 字段A , 字段B from #Tmp;
--删除临时表#Tmp
drop table #Tmp;
SQLServer 删除表中的重复数据,只保留一条记录
最新推荐文章于 2023-11-22 09:40:11 发布