从SQL Server表中删除重复项

时间:2017-01-18 12:31:09

标签: sql sql-server duplicates distinct sql-delete

我的SQL Server表中有一些情况,我有很多重复的记录,如下所示:

 SID    Username                                InQueue
------------------------------------------------------------------    
 162    peeeer2  492    2017-01-18 12:20:21.820   0    354
2791    peeeer2  460    2017-01-11 00:00:00.000   1    NULL

此处不需要的记录是我为其设置InQueue = true的peeeer2用户。我需要删除InQueue列设置为1的所有重复项,另一个标准是用户名实际上是重复的...

表格名称为SearchedUsernames

delete from SearchedUsernames
where Username ??

有人可以帮我解决这个问题吗?

编辑:

@TimSchmelter ty这么多,这就像一个魅力。但是我仍然收到错误。我需要首先删除此表的相邻FK。例如,当我在邻居表中有一个名为UserTransactions的相应FK记录时,如下所示:

 ID      SID                              
----------------  
 162    162
2791    2791    

我需要首先删除此邻居表中的所有记录,然后使用您编写的查询删除重复项。但是,这次我只想删除那些有重复记录且设置InQueue = 0的那些;

所以场景看起来像这样:

  1. 从相邻表UserTransactions中的两个重复项中删除FK记录SID

  2. 然后执行DTV&蒂姆写了一个小小的改动只删除那些重复的记录,并设置了InQueue = 0;

3 个答案:

答案 0 :(得分:5)

WITH cte AS (
    SELECT Username, inqueue,
        ROW_NUMBER() OVER (PARTITION BY Username ORDER BY InQueue ASC) AS RN
    FROM searchedUsernames   
)
DELETE FROM cte
WHERE RN > 1;

如果您害怕多个Inqueue = 0,则使用RANK

WITH cte AS (
    SELECT Username, inqueue,
        RANK() OVER (PARTITION BY Username ORDER BY InQueue ASC) AS RN
    FROM searchedUsernames   
)
DELETE FROM cte
WHERE RN > 1;

答案 1 :(得分:3)

最直观的解决方案可能是:

delete s from SearchedUsernames s
where InQueue = 1 and exists(select * from SearchedUsernames
                             where InQueue = 0 and Username = s.Username)

答案 2 :(得分:1)

你可以像这样使用自我联接:

 DELETE t0
 FROM SearchedUsernames t0
 INNER JOIN SearchedUsernames t1 ON(t0.Username = t1.Username AND t0.IsQueue <> T1.IsQueue)
 WHERE AND t0.IsQueue = 1