你可以用 group by 以及 having ``` SELECT COUNTY, CITY, ZIP FROM MoratoriumLocations WHERE MoratoriumID=20 GROUP BY COUNTY, CITY, ZIP HAVING COUNT(1) >1 ORDER BY County
如果您想获得完整的行详细信息,可以将子查询与 `group by` 以及 `having` 声明
SELECT x.* FROM MoratoriumLocations x INNER JOIN( SELECT COUNTY, CITY, ZIP FROM MoratoriumLocations WHERE MoratoriumID=20 GROUP BY COUNTY, CITY, ZIP HAVING COUNT(1) >1 ) dups ON dups.County = x.County AND dups.City = x.City AND dups.Zip = x.Zip
SELECT ml.*
FROM (SELECT ml.*, COUNT(*) OVER (PARTITION BY County, City, Zip) as cnt
FROM MoratoriumLocations ml
WHERE MoratoriumID = 20
) ml
ORDER BY cnt DESC, County, City, Zip;
3条答案
按热度按时间c86crjj01#
你可以用
group by
以及having
```SELECT COUNTY, CITY, ZIP
FROM MoratoriumLocations
WHERE MoratoriumID=20
GROUP BY COUNTY, CITY, ZIP
HAVING COUNT(1) >1
ORDER BY County
SELECT x.*
FROM MoratoriumLocations x
INNER JOIN(
SELECT COUNTY, CITY, ZIP
FROM MoratoriumLocations
WHERE MoratoriumID=20
GROUP BY COUNTY, CITY, ZIP
HAVING COUNT(1) >1
) dups ON dups.County = x.County
AND dups.City = x.City
AND dups.Zip = x.Zip
t40tm48m2#
有关如何查找DUP,请参见preben的答案。
为了避免DUP,请考虑创建一个唯一索引。
0vvn1miw3#
我建议窗口功能:
这将显示包含重复项的完整行,这可以帮助您更好地理解它们。