SQL Server 删除重复行,查询重复,多记录匹配保留最小行(如何删除 SQL Server 表中的重复行)

https://support.microsoft.com/zh-cn/topic/%E5%A6%82%E4%BD%95%E5%88%A0%E9%99%A4-sql-server-%E8%A1%A8%E4%B8%AD%E7%9A%84%E9%87%8D%E5%A4%8D%E8%A1%8C-23da374f-ab76-4dbe-a43d-5b85e7dc049f

参考微软的文档写:

 DROP TABLE holdkey;

SELECT MAX(ID) ID,TrackingNumberId,StatusName,StatusTime,count(*) as countRow 
INTO holdkey
FROM Tracking_Status GROUP BY TrackingNumberId,StatusName,StatusTime  HAVING COUNT(*)>1

DELETE t FROM Tracking_Status t, holdkey h
WHERE t.ID <> h.ID AND t.TrackingNumberId=h.TrackingNumberId AND t.StatusName=h.StatusName AND t.StatusTime=h.StatusTime

 

posted @ 2021-07-01 17:04  LuoCore  阅读(81)  评论(0编辑  收藏  举报