配置单元ql-分组计数

5m1hhzi4  于 2021-06-25  发布在  Hive
关注(0)|答案(1)|浏览(320)

我是这里的新手,在hiveql中仍然面临很多问题,需要和大家商量。我有一个名为vote table的表,我想计算a、b、c、d的“是”票(很抱歉,我无法发布图片,所以我将其作为链接发送)。
投票表
但这里我只想把a1,a2,a3,a4的计数加在一起;而对于b,c仍将单独计算。我期望的结果是
结果表
我试过的是

select
type,
count(
  case 
          when type = 'A1' and vote = 'yes' then 1
          when type = 'A2' and vote = 'yes' then 1
          when type = 'A3' and vote = 'yes' then 1
          when type = 'A4' and vote = 'yes' then 1
          else vote = 'yes' then 1
)
from vote_table
where …
group by type

我也试过这种方法

if (type in ('A1', 'A2', 'A3', 'A4') and vote = 'yes' then count(*) else (if (vote = 'yes' then count(*)))) as cnt_yes

但两者都不起作用。那么,我想咨询一下这里的Maven,有没有更好的办法呢?谢谢!

osh3o9ms

osh3o9ms1#

按计算类型分组: case when type in ('A1', 'A2', 'A3', 'A4') then 'A' else type end ```
select
case when type in ('A1', 'A2', 'A3', 'A4') then 'A' else type end as type,
sum(case when vote = 'yes' then 1 else 0 end) as number_of_vote
from vote_table
where ...
group by case when type in ('A1', 'A2', 'A3', 'A4') then 'A' else type end

相关问题