hive - 在 Hive 中计算不同的每一列
问题描述
给定下表:
--------------------------------------------------------------------------------------
| browser (col1) | os (col2) | device (col2) | ... | city (col650) |
--------------------------------------------------------------------------------------
| Chrome | Android | Samsung | ... | Berlin |
--------------------------------------------------------------------------------------
| Chrome | Android | Samsung | ... | Cologne |
--------------------------------------------------------------------------------------
| Mozilla | Android | Huawei | ... | Munich |
--------------------------------------------------------------------------------------
| Chrome | Android | Sony | ... | Berlin |
--------------------------------------------------------------------------------------
我想获得每列的不同值:
--------------------------------------------------------------------------------------
| browser (col1) | os (col2) | device (col2) | ... | city (col650) |
--------------------------------------------------------------------------------------
| 2 | 1 | 3 | ... | 4 |
--------------------------------------------------------------------------------------
该表有 650 个不同的列,因此无法在查询中指定每一列。
解决方案
您必须对所有 650 列执行此操作。将排名为 1 的所有行值相加。
select
sum(case when col1Rank=1 then 1 ekse 0 end) as col1,
sum(case when col2Rank=1 then 1 else 0 end) as col2,
sum(case when col3Rank=1 then 1 else 0 end) as col3
from
(
select
row_number() over(partition by col1 order by col1) as col1Rank,
row_number() over(partition by col2 order by col2) as col2Rank,
row_number() over(partition by col3 order by col3) as col3Rank
from table_name
) A;
推荐阅读
- c# - 我应该如何正确连接两个表,以便我可以访问其中一个实体中的属性?
- ms-access - 将一行中的元 ID 自引用到另一行中的 ID,并使用关联的查找填充文本框
- python - 动态输入和动态输出规则的问题
- html - 如何放大一些图像 html 和 css
- python - 尝试使用 SMTP 发送电子邮件时出现 python 错误
- list - 在模型的 foreach 循环或 for 循环中使用文本框
- android - 获取 Android 操作系统分支名称
- regex - 组合几个正则表达式
- php - 为什么 preg_match() 不返回数字格式?
- javascript - 添加 React 无限滚动