SQL返回多个计数语句

时间:2014-10-16 08:35:51

标签: sql count

我有一张桌子,里面有世界各地的滑雪胜地。该表还确定了他们所在的国家和“部门”(美国/欧洲/亚洲等)。我正在尝试确定每个部门的度假村数量。

示例数据:

resort_id, resort_name, country_id, sector_id
3376    Chréa           204  204
3377    Tikjda          204  204
3384    Beidahu         208  205
3481    Canyon Ski Area 225  206
3482    Castle Mountain 225  206
3483    Drumheller      225  206

我需要能够确定每个部门的度假村数量,即:

Sector       Resorts
--------------------
204          2
205          1
206          3

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:6)

select sector_id,
       count(*)
from   resortTableName
group by sector_id

并解决您编辑过的问题:

select sector_id,
       count(distinct resort_name) as resortCount,
       count(distinct country_id) as countryCount
from   resortTableName
group by sector_id

答案 1 :(得分:2)

您只需要count并按行业分组结果:

SELECT   sector_id, COUNT(*)
FROM     resorts
GROUP BY sector_id

答案 2 :(得分:1)

这是一个简单的group By

Select count(*) As Resorts, sector_id as Sector 
from yourtablename 
group by sector_id