我是个新手,在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但这两种方法都不起作用。所以,我想请教一下这里的专家,有没有更好的方法呢?谢谢!
发布于 2020-03-02 01:31:43
按计算类型分组: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 endhttps://stackoverflow.com/questions/60477588
复制相似问题