2014-09-02 96 views
0

我有以下基于employee_id字段标识重复记录的查询。SQL查询识别并删除除最新记录以外的所有内容

SELECT ROW_NUMBER() OVER(PARTITION BY c1.employee_id ORDER BY c1.lastlogon ASC) AS Row 
    ,[DN] 
    ,[first_name] 
    ,[last_name] 
    ,[init] 
    ,[email] 
    ,[title] 
    ,[display_name] 
    ,[department] 
    ,[phone_num] 
    ,[mob_num] 
    ,[fax_num] 
    ,[pager_num] 
    ,[logon] 
    ,[post_code] 
    ,[www] 
    ,[objectSID] 
    ,[disabled] 
    ,[lastlogon] 
    ,[employee_id] 
    ,[acc_type] 
FROM AD_Users_All_Staging c1 
WHERE EXISTS 
(
    SELECT 1 
    FROM AD_Users_All_Staging c2 
    WHERE c2.employee_id = c1.employee_id 
    GROUP BY 
     employee_id 
    HAVING COUNT(1) > 1 -- more than one value 
) 

如何做一个选择只是最新的记录(lastlogon字段值)为它的存在重复(在EMPLOYEE_ID字段值)

的跟进问题是我如何删除所有记录除了最新的记录以外,每个重复吗?

非常感谢

回答

0

由于我没有数据,我不能尝试任何事情容易......但是,这么说,如果你改变了窗口函数使用c.lastlogon Desc而不是升序。然后,您将始终保留第一个记录Row = 1并删除其余的Row > 1

0

你可以使用如下的最新记录:

select uas.* 
from AD_Users_All_Staging uas 
where not exists (select 1 
        from AD_Users_All_Staging uas2 
        where uas2.employee_id = uas.employee_id and 
         uas2.lastlogon > uas.lastlogon 
       ); 

可以使用逆逻辑做delete

select uas.* 
from AD_Users_All_Staging uas 
where exists (select 1 
       from AD_Users_All_Staging uas2 
       where uas2.employee_id = uas.employee_id and 
        uas2.lastlogon > uas.lastlogon 
      ); 
+0

谢谢戈登非常赞赏 – 2014-09-02 21:43:20

0

我做了一些挠头:

我已经试过这它看起来像它给了我它需要的结果:

;WITH cte AS 
(SELECT ROW_NUMBER() OVER(PARTITION BY c1.employee_id ORDER BY c1.lastlogon DESC) AS Row 
    ,[DN] 
    ,[first_name] 
    ,[last_name] 
    ,[init] 
    ,[email] 
    ,[title] 
    ,[display_name] 
    ,[department] 
    ,[phone_num] 
    ,[mob_num] 
    ,[fax_num] 
    ,[pager_num] 
    ,[logon] 
    ,[post_code] 
    ,[www] 
    ,[objectSID] 
    ,[disabled] 
    ,[lastlogon] 
    ,[employee_id] 
    ,[acc_type] 
FROM AD_Users_All_Staging c1 
WHERE EXISTS 
(
    SELECT 1 
    FROM AD_Users_All_Staging c2 
    WHERE c2.employee_id = c1.employee_id 
    GROUP BY 
     employee_id 
    HAVING COUNT(1) > 1 -- more than one value 
) 
) 
SELECT * FROM cte 
WHERE row != 1 

这看起来好吗?

+0

当然,这基本上是我的建议。 – Jim 2014-09-02 21:42:00

+0

非常感谢吉姆 – 2014-09-02 21:42:48

相关问题