apache-spark - 如何通过聚合在 Hive 中透视数据

标签 apache-spark hadoop hive impala

我有一个如下所示的表数据,我想通过聚合来旋转数据。

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 中实现这一目标?

最佳答案

使用 casesum 聚合:

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/

相关文章:

python - Pyspark 朴素贝叶斯批量使用拟合

apache-spark - ProcessingTimeExecutor :66 - Current batch is falling behind. 触发间隔为xxx毫秒,但花费了xxx毫秒

hadoop - pig 中的虚拟列

hadoop - 配置单元插入和加载数据查询不起作用

apache-spark - 在 Windows 中为 pyspark 设置的环境变量

apache-spark - 调用 o67.load : java. lang.NoClassDefFoundError: org/apache/hadoop/fs/staging/StagingDirectoryCapable 时出错

shell - 你如何移动文件而不是 hdfs 中的目录?

hadoop - aqua studio 上的 HIVe sql

shell - 告诉 Impala 忽略错误并继续

sql - 从DOB计算年龄