我有两张桌子(联系人和电子邮件地址),我想知道是否有任何重复,无论是第一个名字还是姓氏或电子邮件地址,但我不确定这是否能让我得到我想要的东西?
DECLARE @lClientId int;
SET @lClientId = @ClientId;
WITH Dups
AS (
SELECT C.ClientId
,C.FirstName
,C.LastName
,EA.Email
,C.ContactId
,C.IsDeleted
FROM Contacts C
LEFT JOIN EmailAddresses EA
ON EA.ParentId = C.ContactId
WHERE C.ClientId = @ClientId
)
SELECT Dups1.ContactId
FROM Dups
INNER JOIN Dups Dups1
ON Dups1.ClientId = Dups.ClientId
AND Dups1.FirstName = Dups.FirstName
AND Dups1.LastName = Dups.LastName
OR Dups1.Email = Dups.Email
WHERE Dups.IsDeleted = 0

答案 0 :(得分:1)
对于像这样的测试数据
Firstname lastname email
a a1 smp
a a1 bmp
c d dmp
c e dmp
复制的输出将为..,您可以通过更改顺序来获取确定性数据
a a1 smp
c e dmp
DECLARE @lClientId int;
SET @lClientId = @ClientId;
WITH Dups
AS (
SELECT C.ClientId
,C.FirstName
,C.LastName
,EA.Email
,C.ContactId
,C.IsDeleted
Row_number() over (partition by firstname,lastname order by(select null)) as fnamerownum,
Row_number() over (partition by email order by(select null)) as emailrownum
FROM Contacts C
LEFT JOIN EmailAddresses EA
ON EA.ParentId = C.ContactId
WHERE C.ClientId = @ClientId
)
select * from dups
where fnamerownum>1 or emailrownum>1
答案 1 :(得分:0)
如果你想要重复的第一个+姓氏或电子邮件列表
你可以
SELECT C.FirstName
, C.LastName
, EA.Email
, count(*)
FROM Contacts C
LEFT JOIN EmailAddresses EA
ON EA.ParentId = C.ContactId
group by C.FirstName
, C.LastName
, EA.Email
having count(*) > 1