从多个列配置单元中计算不同的值

时间:2017-07-21 20:42:01

标签: hive hiveql

我有一张如下表格 Id col1 col2 col3 1 Yes No No 2 No Yes No 3 Yes No No 4 No Yes No

我正在寻找以下列格式显示结果的配置单元查询 Value col1 col2 col3 Yes 2 2 0 No 2 2 4

1 个答案:

答案 0 :(得分:1)

select      val                                     as value
           ,count (case when pos = 0 then 1 end)    as col1 
           ,count (case when pos = 1 then 1 end)    as col2
           ,count (case when pos = 2 then 1 end)    as col3 

from        mytable
            lateral view posexplode(array(col1,col2,col3)) pe

group by   val
+-------+------+------+------+
| value | col1 | col2 | col3 |
+-------+------+------+------+
| No    |    2 |    2 |    4 |
| Yes   |    2 |    2 |    0 |
+-------+------+------+------+