我有一个名为users
的表,其中包含first name
和last name
,我希望保留一条同一first name
和last name
的记录,其他记录将被删除。
例如:
first name: Peter last name: White
first name: Mary last name: White
first name: John last name: Brown
first name: Peter last name: White
执行SQL后:
first name: Peter last name: White
first name: Mary last name: White
first name: John last name: Brown
答案 0 :(得分:2)
SQL Server的一个很好的功能是能够使用CTE进行更新,插入和删除。这是一种方法:
with fordeletion as (
select t.*,
row_number() over (partition by fname, lname order by (select NULL)) as seqnum
from t
)
delete from fordeletion
where seqnum > 1;