我有一个如下所示的表数据,我想通过聚合来旋转数据。
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 - 如何通过聚合在 Hive 中透视数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52191603/