apache-spark - 如何使用聚合在 Hive 中透视数据
问题描述
我有一个像下面这样的表数据,我想用聚合来旋转数据。
ColumnA ColumnB ColumnC
1 complete Yes
1 complete Yes
2 In progress No
2 In progress No
3 Not yet started initiate
3 Not yet started initiate
想要像下面这样旋转
ColumnA Complete In progress Not yet started
1 2 0 0
2 0 2 0
3 0 0 2
无论如何,我们可以在 hive 或 Impala 中实现这一点吗?
解决方案
case
与sum
聚合一起使用:
select ColumnA,
sum(case when ColumnB='complete' then 1 else 0 end) as Complete,
sum(case when ColumnB='In progress' then 1 else 0 end) as In_progress,
sum(case when ColumnB='Not yet started' then 1 else 0 end) as Not_yet_started
from table
group by ColumnA
order by ColumnA --remove if order is not necessary
;
推荐阅读
- apache-spark - 解决 NoClassDefFoundError: org/apache/spark/Logging 异常
- ubuntu - 如何正确配置 telegraf 以写入来自 MQTT 的 InfluxDB 数据
- c - 检查两个数字是否彼此接近
- hadoop - 将整个数据节点添加到 HDFS
- java - 从批处理文件调用蚂蚁
- sql - 带分组的 SQL 数据透视表
- git - 如何撤消“git update-ref -d refs/heads/master”?
- intellij-idea - Goland 自定义 shell 不适用于调试模式
- c# - 订阅和取消订阅单元格按钮单击事件在 iOS 11 中不起作用
- ruby-on-rails - 仅当字段属于其他表行时才存在验证字段