如何删除重复的行

时间:2014-03-03 15:56:48

标签: sql sql-server-2008-r2 duplicate-removal

我使用的是SQL Server 2008 R2。

我发现这个脚本有重复的行:

SELECT CLDest, CdClient,
 COUNT(CLDest) AS NumOccurrences
FROM DEST
GROUP BY CLDest,CdClient
HAVING ( COUNT(CLDest) > 1 )

它返回48个条目

在删除之前,我必须确保删除双打:

SELECT DEST.CdClient
      ,DEST.CLDest
FROM [Soft8Exp_Client_WEB].[dbo].[DEST]
WHERE DEST.CdClient IN (SELECT  CdClient
                        FROM DEST
                        GROUP BY CdClient
                        HAVING (COUNT(CLDest) > 1) )
  AND DEST.CLDest IN (SELECT CLDest
                      FROM DEST
                      GROUP BY CLDest
                      HAVING (COUNT(CLDest) > 1) )

此查询返回64628个条目

所以我认为我的选择是错误的。

3 个答案:

答案 0 :(得分:2)

SQL Server具有可更新CTE的nice属性。与函数row_number()结合使用时,您可以执行以下操作:

with todelete as (
      select d.*,
             row_number() over (partition by CLDest, CdClient order by newid()) as seqnum
      from dest d
     )
delete from todelete
    where seqnum > 1;

此版本将随机删除其中一个副本。它的作用是为具有相同值的行分配一个序号,并删除除了找到的第一个之外的所有行。如果您想按日期保留某些内容,请在order by

中使用其他表达式

答案 1 :(得分:1)

;WITH Duplicates
 AS
   (
   SELECT CLDest
        , CdClient
        , ROW_NUMBER() OVER (PARTITION BY CLDest, CdClient ORDER BY CdClient) AS Rn
   FROM DEST
   )
DELETE FROM Duplicates
WHERE RN > 1

答案 2 :(得分:0)

SELECT DEST.CdClient,DEST.CLDest 
FROM [Soft8Exp_Client_WEB].[dbo].[DEST]
WHERE DEST.CdClient+DEST.CLDest 
  IN (
    SELECT CdClient+CLDest FROM DEST GROUP BY CLDest HAVING ( COUNT(CLDest) > 1 )
   )