我有一个用于检查重复地址的查询(基于具有相同的地址行1,地址行2和邮政编码),但如果transactionType是“Disconnect”之一,则它不应显示记录。 DeEnrollDeceased','DeEnrollLeaving','DeEnrollFailedRecertification','DeEnrollNonUsage'或iehFlag未设置为0。
因此,如果两个记录具有相同的地址,但是一个是“Disconnect”而另一个不是,则不应返回任何记录。此外,如果两个记录具有相同的地址但是一个是iehFlag = 0且一个是iehFlag = 1,那么两者都不应该返回。目前我在两种情况下都返回了。
我非常感谢我的查询出错的任何帮助
SELECT 'Address' DuplicateType, iehFlag, transactionType, AccountNumber, AccountType, firstName, lastName, last4ssn, tribalId, dob, phoneNumber
FROM table a,
(SELECT primaryAddress1, COALESCE(primaryAddress2, '') address2, primaryZipCode
FROM table
WHERE transactionType NOT IN ('Disconnect', 'DeEnrollDeceased', 'DeEnrollLeaving', 'DeEnrollFailedRecertification', 'DeEnrollNonUsage')
AND iehFlag = 0
GROUP BY primaryAddress1, primaryAddress2, primaryZipCode
HAVING COUNT(AccountNumber) > 1) b
WHERE a.primaryAddress1 = b.primaryAddress1
AND COALESCE(a.primaryAddress2, '') = b.address2
AND a.primaryZipCode = b.primaryZipCode
ORDER BY a.primaryAddress1
答案 0 :(得分:0)
请改为尝试:
SELECT 'Address' DuplicateType, iehFlag, transactionType, AccountNumber, AccountType, firstName, lastName, last4ssn, tribalId, dob, phoneNumber
FROM table a,
(SELECT primaryAddress1, COALESCE(primaryAddress2, '') address2, primaryZipCode, COUNT(*)
FROM table
WHERE iehFlag = 0
GROUP BY primaryAddress1, primaryAddress2, primaryZipCode
HAVING COUNT(*) > 1) b
WHERE a.primaryAddress1 = b.primaryAddress1
AND COALESCE(a.primaryAddress2, '') = b.address2
AND a.primaryZipCode = b.primaryZipCode
AND transactionType NOT IN ('Disconnect', 'DeEnrollDeceased', 'DeEnrollLeaving', 'DeEnrollFailedRecertification', 'DeEnrollNonUsage')
ORDER BY a.primaryAddress1
在select子句中使用Count(*)并且Having子句应该正确地标记具有多个实例的数据。如果有什么不清楚,请告诉我。