假如今天添加了 1批数据 每批重复插入4次
字段名name为唯一
cte写法 先查出来 取出需要保留的然后关联查询丢弃不需要的
;WITH KeepDataTable (ID,name) AS ( SELECT MAX(id) id,name FROM dbo.MYName WHERE CONVERT(date, CreatedDate) >= '2025-01-08' GROUP BY name ) DELETE FROM MYName WHERE id IN ( SELECT a.id FROM MYName a INNER JOIN KeepDataTable b ON a.id<>b.id AND a.name =b.name WHERE CONVERT(date, a.CreatedDate) >= '2025-01-08' )