突出显示重复值

Highlighting duplicate values

我有以下脚本:

   SELECT
   Reference.ReferenceNumber AS [Reference.ReferenceNumber)
   Reference.LastName
   Reference.FirstName
   Address.ReferenceNumber AS [Address.ReferenceNumber]
   Address.Address1
   Address.Address2
   Address.Address3
   Address.Address4
   Address.ZipCode
   Telephone.ReferenceNumber AS [Telephone.ReferenceNumber]
   Telephone.TelephoneNumber
   Email.ReferenceNumber AS [Email.ReferenceNumber]
   Email.EmailAddress
   FROM
   Reference
   INNER JOIN Address
   ON Reference.ReferenceNumber = Reference.ContactNumber
   LEFT OUTER JOIN Telephone
   ON Reference.ReferenceNumber = Telephone.ReferenceNumber
   LEFT OUTER Join Email
   ON Reference.ReferenceNumber = Email.ReferenceNumber

这会提取所有客户,以及他们的地址、电子邮件和 phone 号码。

然而,有些人的姓氏和地址重复。

请您建议修改脚本以显示哪些记录是重复的?理想情况下,我希望输出显示姓氏、地址 1 和邮政编码相同的位置,并将输出限制为仅重复行。 非常感谢。

类似

SELECT
Reference.ReferenceNumber AS [Reference.ReferenceNumber),
case when vrln.c is null then Reference.LastName else 'dup: ' + Reference.LastName end as LastName, 
Reference.FirstName,
Address.ReferenceNumber AS [Address.ReferenceNumber],
Address.Address1,
Address.Address2,
Address.Address3,
Address.Address4,
Address.ZipCode,
Telephone.ReferenceNumber AS [Telephone.ReferenceNumber],
Telephone.TelephoneNumber,
Email.ReferenceNumber AS [Email.ReferenceNumber],
Email.EmailAddress
FROM
Reference
INNER JOIN Address
ON Reference.ReferenceNumber = Reference.ContactNumber
LEFT OUTER JOIN Telephone
ON Reference.ReferenceNumber = Telephone.ReferenceNumber
LEFT OUTER Join Email
ON Reference.ReferenceNumber = Email.ReferenceNumber

left join (
    select rln.LastName, add.Address1, add.ZipCode, count(rln.LastName) as c
    from 
        Reference rln 
        INNER JOIN Address add
            ON rln.ReferenceNumber = add.ReferenceNumber
    group by rln.LastName, add.Address1, add.ZipCode 
    having count(rln.LastName) > 1
) vrln on Reference.LastName = vrln.LastName and Address.Address1 = vrln.Address1 and Address.ZipCode = vrln.ZipCode

where vrln.c is not null

但是,要查找重复项:

select rln.LastName, add.Address1, add.ZipCode, count(rln.LastName) as c
from 
    Reference rln 
    INNER JOIN Address add
        ON rln.ReferenceNumber = add.ReferenceNumber
group by rln.LastName, add.Address1, add.ZipCode 
having count(rln.LastName) > 1

够了