SQL:查找重复项,具有不同的字段
SQL: find duplicates, with a different field
我必须在 Access table 中找到重复项,其中一个字段不同。
我将尝试解释:假设有这个数据集
ID Country CountryB Customer
====================================================
1 Italy Austria James
2 Italy Austria James
3 USA Austria James
我必须找到所有具有重复的 CountryB 和 Customer,但具有不同 Country 的记录。
例如,对于上面的数据,ID 1 和 2 NOT 重复(因为它们来自同一个国家/地区),而 1 和 3(或 2 和 3 ) 是。
我得到的 "best" 查询如下:
SELECT COUNT(*), CountryB, Customer FROM
(SELECT MIN(ID) as MinID, Country, CountryB, Customer FROM myTable GROUP BY Country, CountryB, Customer)
GROUP BY CountryB, Customer
HAVING COUNT(*)>1
无论如何,我不确定这是否是最明智的选择。
此外,由于我需要 "mark" 所有重复项,因此我必须做更多的事情,例如:
SELECT ID, a.Country, a.CountryB, a.Customer FROM myTable a
INNER JOIN
(
SELECT COUNT(*), CountryB, Customer FROM
(SELECT MIN(ID) as MinID, Country, CountryB, Customer FROM myTable GROUP BY Country, CountryB, Customer)
GROUP BY CountryB, Customer
HAVING COUNT(*)>1
) dt
ON a.Country=dt.Country and a.CountryB=dt.CountryB and a.Customer=dt.Customer
非常感谢任何有关此方法的建议。
我终于找到了解决办法。
正确的解决方案在这个答案中:
SELECT DISTINCT HAVING Count unique conditions
适应这个版本,因为我使用的是 Access 2010:
Count Distinct in a Group By aggregate function in Access 2007 SQL
因此,在我上面的示例 table 中,我可以使用此查询来查找重复记录:
SELECT CountryB, Customer, Count(cd.Country)
FROM (SELECT DISTINCT Country, CountryB, Customer FROM myTable) AS cd
GROUP BY CountryB, Customer
HAVING COUNT(*) > 1
或此查询以查找重复记录的所有 ID:
SELECT ID FROM myTable a INNER JOIN
(
SELECT CountryB, Customer, Count(cd.Country)
FROM (SELECT DISTINCT Country, CountryB, Customer FROM myTable) AS cd
GROUP BY CountryB, Customer
HAVING COUNT(*) > 1
) dt
ON a.CountryB=dt.CountryB AND a.Customer=dt.Customer
我必须在 Access table 中找到重复项,其中一个字段不同。 我将尝试解释:假设有这个数据集
ID Country CountryB Customer
====================================================
1 Italy Austria James
2 Italy Austria James
3 USA Austria James
我必须找到所有具有重复的 CountryB 和 Customer,但具有不同 Country 的记录。
例如,对于上面的数据,ID 1 和 2 NOT 重复(因为它们来自同一个国家/地区),而 1 和 3(或 2 和 3 ) 是。 我得到的 "best" 查询如下:
SELECT COUNT(*), CountryB, Customer FROM
(SELECT MIN(ID) as MinID, Country, CountryB, Customer FROM myTable GROUP BY Country, CountryB, Customer)
GROUP BY CountryB, Customer
HAVING COUNT(*)>1
无论如何,我不确定这是否是最明智的选择。 此外,由于我需要 "mark" 所有重复项,因此我必须做更多的事情,例如:
SELECT ID, a.Country, a.CountryB, a.Customer FROM myTable a
INNER JOIN
(
SELECT COUNT(*), CountryB, Customer FROM
(SELECT MIN(ID) as MinID, Country, CountryB, Customer FROM myTable GROUP BY Country, CountryB, Customer)
GROUP BY CountryB, Customer
HAVING COUNT(*)>1
) dt
ON a.Country=dt.Country and a.CountryB=dt.CountryB and a.Customer=dt.Customer
非常感谢任何有关此方法的建议。
我终于找到了解决办法。 正确的解决方案在这个答案中:
SELECT DISTINCT HAVING Count unique conditions
适应这个版本,因为我使用的是 Access 2010:
Count Distinct in a Group By aggregate function in Access 2007 SQL
因此,在我上面的示例 table 中,我可以使用此查询来查找重复记录:
SELECT CountryB, Customer, Count(cd.Country)
FROM (SELECT DISTINCT Country, CountryB, Customer FROM myTable) AS cd
GROUP BY CountryB, Customer
HAVING COUNT(*) > 1
或此查询以查找重复记录的所有 ID:
SELECT ID FROM myTable a INNER JOIN
(
SELECT CountryB, Customer, Count(cd.Country)
FROM (SELECT DISTINCT Country, CountryB, Customer FROM myTable) AS cd
GROUP BY CountryB, Customer
HAVING COUNT(*) > 1
) dt
ON a.CountryB=dt.CountryB AND a.Customer=dt.Customer