如何使用SQL删除重复的非唯一数据?

时间:2013-10-29 22:35:54

标签: sql sql-server-2008

我有一个名为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

1 个答案:

答案 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;