HIVEQL / HIVE查找列中最常见的字段

时间:2016-04-20 17:45:21

标签: sql hive

    DATE      WindDirection
    1/1/2000  SW
    1/2/2000  SW
    1/3/2000  SW
    1/4/2000  NW
    1/5/2000  NW

下面的问题

 Every day is unqiue, and wind direction is not unique, SO now we are trying    to get the COUNT of the most COMMON wind direction

我的查询是

SELECT Wind_Direction,COUNT(Wind_Direction) FROM Weather
GROUP BY DISTINCT(Wind_Direction);

逻辑是找到DISTINCT WindDirections,有7个然后就是 按WindDirection分组并应用计数

2 个答案:

答案 0 :(得分:2)

按发生次数排序时每个方向的发生次数分组,并限制1以使其发生在顶部

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;

答案 1 :(得分:0)

您可以尝试使用hive分析函数执行逻辑:

with q1 as (select wind_direction, count(wind_direction) over (partiton by wind_direction) as total_counts from weather) select distinct wind_direction, total_counts from q1;