1
我的SQL字符串如下使用與GROUPING HAVING SETS
SELECT Country, City, COUNT(*) AS [Count]
FROM CountriesAndCities
GROUP BY GROUPING SETS ((Country, City), (Country))
ORDER BY Country, City
我得到的結果如下
Country City CountryCount
---------- ---------- ------------
France NULL 4
France Paris 4
Spain NULL 6
Spain Barcelona 3
Spain Madrid 3
如果國家已經得到了只有一個城市的記錄,我可以得到結果如下使用HAVING
Country City CountryCount
---------- ---------- ------------
France Paris 4
Spain NULL 6
Spain Barcelona 3
Spain Madrid 3
非常感謝你,但是使用這個查詢只返回NULL值的COUNTRY行 – Kerberos 2015-04-05 22:37:27
對不起,我弄錯了;)我已經更新了它。請參閱https://msdn.microsoft.com/en-us/library/ms178544.aspx – MatBailie 2015-04-05 22:38:32