使用where子句计算列

时间:2018-04-09 16:30:57

标签: sql hive hiveql

有没有办法计算Hive中每行具有特定值的列数。 enter image description here 我有数据看起来像输入,我想计算有多少列有价值' a'以及有多少列有价值' b'并获得输出'输出'。 有没有办法用Hive查询来完成这个?

2 个答案:

答案 0 :(得分:2)

Hive中的一种方法是:

select ( (case when cl_1 = 'a' then 1 else 0 end) +
         (case when cl_2 = 'a' then 1 else 0 end) +
         (case when cl_3 = 'a' then 1 else 0 end) +
         (case when cl_4 = 'a' then 1 else 0 end) +
         (case when cl_5 = 'a' then 1 else 0 end)
       ) as count_a,
       ( (case when cl_1 = 'b' then 1 else 0 end) +
         (case when cl_2 = 'b' then 1 else 0 end) +
         (case when cl_3 = 'b' then 1 else 0 end) +
         (case when cl_4 = 'b' then 1 else 0 end) +
         (case when cl_5 = 'b' then 1 else 0 end)
       ) as count_b
from t;

要获得总计数,我建议您使用子查询并添加count_acount_b

答案 1 :(得分:2)

lateral viewexplode一起使用,并对其进行汇总。

select id
,sum(cast(col='a' as int)) as cnt_a
,sum(cast(col='b' as int)) as cnt_b
,sum(cast(col in ('a','b') as int)) as cnt_total
from tbl
lateral view explode(array(ci_1,ci_2,ci_3,ci_4,ci_5)) tbl as col
group by id