如何从我的查询中引用的表中删除记录,例如,下面是我的查询,它返回正确的结果数量,但我想从查询中引用的同一个表中删除这些记录
;with cte as (select *,
row_number() over (partition by c.[Trust Discharge], c.[AE Admission], c.[NHS Number]
order by c.[Hospital Number]) as Rn,
count(*) over (partition by c.[Trust Discharge], c.[AE Admission], c.[NHS Number]) as cntDups
from CommDB.dbo.tblNHFDArchive as c)
Select * from cte
Where cte.Rn>1 and cntDups >1
答案 0 :(得分:1)
因为您已经可以通过查询Select * from cte Where cte.Rn>1 and cntDups >1
来选择行,您可以通过运行delete from your_table where unique_column in (Select unique_column from cte Where cte.Rn>1 and cntDups >1)
来删除它们
请注意, unique_column 是表格中不能包含重复值的列, your_table 是行所在的表格。
如果正在制作,请不要先忘记备份。