删除SQL Server中的成对重复项

时间:2014-06-25 12:37:14

标签: sql sql-server

在SQL Server上,以下sql计算每个成对OrgID和FeatureID的相同行数:

SELECT        TOP (100) PERCENT COUNT(1) AS Total, OrgID, FeatureID
FROM            Organisation.Tag AS t
GROUP BY OrgID, FeatureID
HAVING        (COUNT(1) > 1)
ORDER BY Total DESC

但是,我对重复不感兴趣。删除重复项的SQL是什么?

表格本身包含以下字段:

[TagID] [int] IDENTITY(1,1) NOT NULL,
[LastModified] [datetime] NOT NULL,
[OrgID] [int] NULL,
[FeatureID] [int] NOT NULL

1 个答案:

答案 0 :(得分:1)

这是一种方式:

with todelete as (
      select t.*, row_number() over (partition by ordid, featureid
                                     order by lastmodified desc) as seqnum
      from organization.tag
     )
delete from todelete
    where seqnum > 1;