0

所以一个关于 SQL 查询的快速问题,

我有一个包含 VendorNames、VendorCity 和 VendorStates 的表。我想拉出这 3 列,但每个城市和州只返回一个结果。换句话说,我希望查询返回供应商与另一供应商没有共同城市和州的结果。

表名是 Vendors,上面的列名是正确的。

4

2 回答 2

2
SELECT VendorCity, VendorStates, min(VendorNames)
FROM Vendors
GROUP BY VendorCity, VendorStates
HAVING count(VendorNames) = 1
于 2013-07-28T17:04:23.130 回答
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
于 2013-07-28T17:04:19.050 回答