sql - 从表中的计数(列)在蜂巢中排名
问题描述
如果我想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
解决方案
不需要子查询:
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;
推荐阅读
- java - 如何使用 HTreeMap 填充列表
- android - 在 WebView 中显示 NEST 相机视频
- php - 如何在soap请求后隔离xml标签的内容
- java - 如何在java中使用pdfbox api从pdf中获取特定文本
- themes - 无法卸载 NetSuite SuiteCommerce 基本主题
- javascript - c3.js/bb.js 隐藏一行
- javascript - 将带有对象的数组转换为带有来自该对象的特定键的数组?
- json - 如何检查 Newtonsoft.Json.Linq.JArray 如果为 null 或获取长度
- c - 允许编译器优化 realloc 吗?
- vb.net - 计算出时间跨度,但需要分钟 00:20:00 (HH:mm:ss)