首页 > 解决方案 > 从表中的计数(列)在蜂巢中排名

问题描述

如果我想count(user_name), country从 hive 中的表中进行选择。我应该使用什么命令来获得最多用户名的前 2 个国家/地区的结果?

如何使用排名功能?

id | user_name | country
 1 | a         | UK
 2 | b         | US
 3 | c         | AUS
 4 | d         | ITA
 5 | e         | UK
 6 | f         | US

结果应该是:

rank| num_user_name | country
 1  | 2             | US
 1  | 2             | UK
 2  | 1             | ITA
 2  | 1             | AUS

标签: sqlhiverank

解决方案


不需要子查询:

select dense_rank() over (order by count(*)) as rank,
       country, 
       count(*) as num_user_name
from t
group by country
order by count(*) desc, country;

推荐阅读