HIVE/HiveQL 获取最大计数
HIVE/HiveQL Get Max count
样本数据
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;
目前这个查询有效,但是它输出了ALL count的计数,我只对每种风向的计数感兴趣,它输出south并输出170000但答案只有10,000。
我知道计数的使用方式有问题,我认为它必须指定一个别名并按特定的方式计数 wind_direction 但我无法将其转换为语法
您似乎需要数据中最常见的风向
select wind_direction, count(*) as cnt
from weather
group by wind_direction
order by cnt desc
limit 1;
如果有多个风向具有相同的最大计数,则获取最大计数并将其用于having clause以获得最常见的风向
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
)
样本数据
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;
目前这个查询有效,但是它输出了ALL count的计数,我只对每种风向的计数感兴趣,它输出south并输出170000但答案只有10,000。
我知道计数的使用方式有问题,我认为它必须指定一个别名并按特定的方式计数 wind_direction 但我无法将其转换为语法
您似乎需要数据中最常见的风向
select wind_direction, count(*) as cnt
from weather
group by wind_direction
order by cnt desc
limit 1;
如果有多个风向具有相同的最大计数,则获取最大计数并将其用于having clause以获得最常见的风向
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
)