所以关于SQL查询的快速问题,
我有一个包含VendorNames,VendorCity和VendorStates的表。我想拉出这3列,但每个城市和州只返回一个结果。换句话说,我希望查询返回供应商没有与其他供应商共同的城市和州的结果。
表名是Vendors,上面的列名是正确的。
答案 0 :(得分:2)
SELECT VendorCity, VendorStates, min(VendorNames)
FROM Vendors
GROUP BY VendorCity, VendorStates
HAVING count(VendorNames) = 1
答案 1 :(得分:0)
SELECT V.*
FROM VendorTable V
JOIN
(
SELECT VendorCity,
VendorStates,
Count(*) AS NoOfVendorNames
FROM VendorTable
GROUP BY VendorCity,
VendorStates
) T
ON V.VendorCity = T.VendorCity
AND V.VendorStates = T.VendorStates
WHERE T.NoOfVendorNames = 1