我今天在我的一个应用程序中发现了一些奇怪的东西 - 实际上有人向我指出了这一点。我有一个包含一大堆客户的表,让用户运行报告以查找潜在的重复项。这是一个初步的重复搜索,所以我按照名字和姓氏的前几个字符进行搜索。
假设我有以下客户列表:
tblA
ClientID FName LName
1 Carol Villa
2 Carol Villa
3 Gainy Brimes
4 Gainy Brimes
这是我正在使用的查询:
select ClientID, FName, LName
from (select t.*, count(*) over (partition by (substring(FName,0,4)), (substring(LName,0,4)), dob) as cnt from tblClients t ) t
where cnt > 1
order by FName, LName
如果我对这个表运行这个查询,我只会得到这些记录:
ClientID FName LName
1 Carol Villa
2 Carol Villa
它没有向我展示Gainy Brimes,有什么原因吗?
答案已经被库什自己发现了。
由于 DOB 用于窗口函数的分区,因此不匹配的 DOB 可能会忽略一些“可疑”重复项。
但这里有一个小测试脚本来演示这种效果:
declare @tblClients table (ClientID int, FName varchar(30), LName varchar(30), dob int);
insert into @tblClients (ClientID, FName, LName, dob) values
(1,'Carol','Villa',100)
,(2,'Carina','Vilpo',100)
,(3,'Garcy','Brimes',100)
,(4,'Garry','Bricks',200)
;
select ClientID, FName, LName
, dob, cnt, rn
, CASE WHEN cnt > 1 THEN 'Has Partial Dups' ELSE 'No Partials Dups' END as ThereBeDups
from
(
select ClientID, FName, LName, dob,
count(*) over (partition by dob, substring(FName,1,3), substring(LName,1,3)) as cnt,
row_number() over (partition by dob, substring(FName,1,3), substring(LName,1,2) order by ClientID desc) as rn
from @tblClients
) t
--where cnt > 1
order by FName, LName;
结果:
ClientID FName LName dob cnt rn ThereBeDups
-------- ----- ----- --- --- -- ----------------
2 Carina Vilpo 100 2 1 Has Partial Dups
1 Carol Villa 100 2 2 Has Partial Dups
3 Garcy Brimes 100 1 1 No Partial Dups
4 Garry Bricks 200 1 1 No Partial Dups
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句