我有以下代码
SELECT
PhysicalAddressID
FROM
tblPhysicalAddresses as a
WHERE EXISTS
(
SELECT
PhysicalAddressID
FROM
tblPhysicalAddresses as b
WHERE
a.PhysicalAddressID <> b.PhysicalAddressID AND
a.Address1 = b.Address1 AND
a.Address2 = b.Address2 AND
a.Address3 = b.Address3 AND
a.City = b.City AND
a.CountryID = b.CountryID AND
a.PersonID = b.PersonID AND
a.PhysicalAddressTypeID = b.PhysicalAddressTypeID AND
a.PostalCode = b.PostalCode AnD
a.Province = b.Province AND
a.StateID = b.StateID AND
a.ZipCode = b.ZipCode
)
我可以直观地查看数据并查看是否存在匹配项。但是,当我运行查询时,它什么都不返回。这可能是由于可空列的比较?如果是这样,那么替代方案。目标是找到重复的行。
例如,列的列为null的实例。找不到匹配项,尽管它们都是空的。
基本上我有80个具有相同模式的数据库。我决定将它们合并为一个。但是有重复的地址。因此,我的目标是合并重复的地址。
答案 0 :(得分:3)
您可以使用this technique将两个对应的空值评估为相等
WHERE
a.PhysicalAddressID <> b.PhysicalAddressID
AND EXISTS (SELECT a.Address1, a.Address2
INTERSECT
SELECT b.Address1, b.Address2)
(剩下的栏目留给你填写)
答案 1 :(得分:2)
我建议将其作为聚合:
SELECT a.Address1, a.Address2, a.Address3, a.City, a.CountryID, a.PersonID,
a.PhysicalAddressTypeID, a.PostalCode, a.Province, a.StateID, a.ZipCode,
COUNT(*) as NumPhysicalAddresses,
MIN(a.PhysicalAddressId), MAX(a.PhysicalAddressId)
FROM tblPhysicalAddresses a
GROUP BY a.Address1, a.Address2, a.Address3, a.City, a.CountryID, a.PersonID,
a.PhysicalAddressTypeID, a.PostalCode, a.Province, a.StateID, a.ZipCode
HAVING COUNT(*) > 1;
在找不到比赛时你可能面临许多问题:
NULL
(group by
版本将会捕获)。答案 2 :(得分:1)
不清楚你想要什么,但这可能是它
SELECT a.PhysicalAddressID
FROM tblPhysicalAddresses as b
JOIN tblPhysicalAddresses as b
ON a.PhysicalAddressID <> b.PhysicalAddressID
AND (a.Address1 = b.Address1 or (a.Address1 is null and b.Address1 is null))
AND (a.Address2 = b.Address2 or (a.Address2 is null and a.Address2 is null))
AND (a.Address3 = b.Address3 or (a.Address2 is null and b.Address3 is null))
AND ...