如何比较单个表中的两行

时间:2011-09-16 11:16:02

标签: sql-server-2008

我的员工登台表包含firstname, middlename, lastname, department, effectivedate, canceldateprocessdate

等列

我必须将每一行与其他行进行比较以查找重复项,如果两行匹配,则必须选择具有更长的处理日期的行。

我正在使用带有Dense_rank函数的CTE来查找重复项,但我没有得到如何比较同一个表中的行。

提前致谢..

1 个答案:

答案 0 :(得分:3)

这将找到具有更长处理日期的重复记录

select s1.*
from staging s1
join staging s2
    on s1.firstname = s2.firstname
    and s1.middlename = s2.middlename
    and s1.lastname = s2.lastname
    and s1.department = s2.department
    -- compare other columns that make records "duplicates" as appropriate
    and s1.processdate > s2.processdate; -- this makes the s1 record the latest