繁体   English   中英

SQL在同一条语句的不同列上进行多次计数

[英]SQL multiple counts on different columns in the same statement

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

样本数据 :

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       226     206
3933       Uuperi           240     207
3934       Salomonkallio    240     207
3935       Chabanon         241     207
3936       Le Fanget        242     207

我需要能够确定每个部门中的度假胜地和国家/地区的数量,即:

Sector    Resorts   Countries
-----------------------------
204          2         2
205          1         1
206          3         2
207          4         3

我需要的陈述大致如下:

select sector_id,
       count(*),
       // count the number of countries in each sector
from   resorts
group by sector_id

任何帮助将不胜感激,谢谢。

我想你想要count(distinct)

select sector_id, count(distinct resort_id) as NumResorts,
       count(distinct country_id) as NumCountries
from resorts
group by sector_id;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM