如何从单个表中获取重复记录取决于列值?

时间:2015-06-12 13:20:04

标签: sql-server tsql sql-server-2008-r2

我有这样的表:

declare @t1 table(Pat_Ref int,Fname VARCHAR(20), Sname VARCHAR(20),minor VARCHAR(1),SourceSys VARCHAR(40),regdt datetime)

insert into @t1 
values   (111,'John', 'Wayne','N','ick','2015-06-09 21:31:09.253')
        ,(111,'John', 'Wayne','N','ick','2014-05-09 21:31:09.253')
        ,(111,'John', 'Wayne',null,'hpk','2015-04-09 21:31:09.253')

        ,(112,'Jill', 'Smith','N','ick','2015-01-08 21:31:09.253')
        ,(112,'Jill', 'Smith',null,'hpk','2015-05-01 21:31:09.253')

        ,(113,'Bill', 'Peyton','N','ick','2015-06-09 21:31:09.253')

        ,(114,'Gill', 'Peyton','N','hpk','2015-06-09 21:31:09.253')
        ,(114,'Gill', 'Peyton','N','hpk','2015-06-12 21:31:09.253')
        ,(114,'Gill', 'Peyton','N','ick','2006-10-22 21:31:09.253')

        ,(115,'Billy', 'Peyton','N','hpk','2015-06-09 21:31:09.253')

        ,(116,'William', 'nixon','N','ick','2015-06-09 21:31:09.253')
        ,(116,'William', 'nixon','N','ick','2015-06-09 21:31:09.253')

我希望基于Pat_Ref的重复记录以及ick列上hpkSourceSys之间的值。如果找到重复记录行必须是最近日期regdt

Note:从上表Pat_Ref=116开始,有两行,但不能重复,因为其SourceSys值相同ick。如果该值在行上为ick而在另一行上为hpk,那么它可能会成为重复行。

我想要这样的结果;

Pat_Ref     Fname   Sname   minor   SourceSys   regdt
111         John    Wayne   N       ick         2015-06-09 21:31:09.253
112         Jill    Smith   NULL    hpk         2015-05-01 21:31:09.253
114         Gill    Peyton  N       hpk         2015-06-12 21:31:09.253

任何帮助。感谢

1 个答案:

答案 0 :(得分:1)

我希望我理解正确,这是我提出的查询,而且似乎确实会返回预期的结果:

SELECT Pat_Ref, Fname, Sname, minor, SourceSys, regdt
FROM (
    SELECT ROW_NUMBER() OVER (PARTITION BY Pat_ReF ORDER BY regdt DESC) AS RN, *
    FROM @t1
) AS T
CROSS APPLY (
    SELECT 1
    FROM @t1 AS T2
    WHERE T2.Pat_Ref = T.Pat_Ref
        AND T2.SourceSys BETWEEN 'hck' AND 'ick'
    GROUP BY T2.Pat_Ref
    HAVING COUNT(DISTINCT T2.SourceSys) > 1
    ) AS T2(UQ)
WHERE T.RN = 1;

<强>说明: 我使用ROW_NUMBER()为每个regdt获取最新的Pat_Ref,以防需要过滤。

我正在使用CROSS APPLY为每个COUNT() Pat_Ref获取DISTINCT SourceSys,如果我理解正确,则必须与ICK和HCK相匹配。

有任何问题 - 请告诉我。