0

我有一个名为 的表users,其中有first namelast name,我想保留一条相同的记录first namelast 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
4

1 回答 1

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;
于 2013-10-29T22:39:52.700 回答