样本数据
DATE WindDirection
1/1/2000 SW
1/2/2000 SW
1/3/2000 SW
1/4/2000 NW
1/5/2000 NW
下面的问题
每一天都是独一无二的,风向不是唯一的,所以现在我们正试图获得最常见风向的COUNT
select w.wind_direction as most_common_wd
from (
select wind_direction, count(*) as cnt
from weather
group by wind_direction
order by cnt desc
) w
limit 1;
目前这个查询有效,但它输出所有计数的计数,我只对每种风向的计数感兴趣,它输出南方并输出170000但答案只有10,000。
我知道使用计数的方式有问题,我认为必须指定一个别名并通过specfic wind_direction计算,但我无法将其转换为语法
答案 0 :(得分:2)
您希望从数据中获得最常见的风向
select wind_direction, count(*) as cnt
from weather
group by wind_direction
order by cnt desc
limit 1;
如果有多个风向具有相同的最大计数,则获取最大计数并在lines 83-171中使用它以获得最常见的风向
select wind_direction
from weather
group by wind_direction
having count(*) = ( select max(c) from
(
select wind_direction,count(*) c
from weather
group by wind_direction
) a
)