我有这样的表:
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
列上hpk
和SourceSys
之间的值。如果找到重复记录行必须是最近日期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
任何帮助。感谢
答案 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相匹配。
有任何问题 - 请告诉我。