您的位置:首页 > 运维架构

Hive分组统计前top N条记录

2017-01-22 18:45 351 查看
参考这篇博客而写:http://blog.csdn.net/longshenlmj/article/details/50525385

本 Hive 语句的目的是统计中国每个省份下所有城市记录出现总次数为前5的结果。

hive -e "
select bb.*
from
(
select country_name,
province_name,
city_name,
pv_cnt,
row_number() over (partition by country_name,province_name order by pv_cnt desc) rank_code
from
(select b.country_name,
b.province_name,
b.city_name,
count(1) as pv_cnt
from table_a a
join table_b b
on a.area_id = b.area_id
where a.dt = '某天' and b.country_name = '中国'
group by b.country_name,
b.province_name,
b.city_name ) aa
) bb
where bb.rank_code <= 5;"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hive topn sql 分组