22,294
社区成员
发帖
与我相关
我的任务
分享
delete #temptable where id in (select min(id) as id,xx,yy,zz,aa from #temptable group by xx,yy,zz,aa)
with cte(id,xx,yy,zz,aa)
as
(
select 1,'haha','2015-01-01',5,0 union all
select 2,'hahaha','2015-02-02',7,0 union all
select 3,'haha','2015-01-01',5,0 union all
select 4,'hahaha','2015-02-02',7,0 union all
select 5,'ha','2015-02-02',7,0
)
select min(id) as id,xx,yy,zz,aa from cte group by xx,yy,zz,aa
order by id
DELETE #temptable
WHERE EXISTS (
SELECT *
FROM #temptable t
WHERE t.id < #temptable.id
AND t.xx = #temptable.xx
AND t.yy = #temptable.yy
AND t.zz = #temptable.zz
AND t.aa = #temptable.aa
)
SELECT * FROM #temptable Id xx yy zz aa
----------- ---------- ---------- ----------- -----------
1 haha 2015-01-01 5 0
2 hahaha 2015-02-02 7 0
5 ha 2015-02-02 7 0