首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HiveQL -分组计数

HiveQL -分组计数
EN

Stack Overflow用户
提问于 2020-03-02 01:08:10
回答 1查看 34关注 0票数 2

我是个新手,在HiveQL上还面临着很多问题,需要和大家商量一下。我有一个名为Vote Table的表格,我想计算A、B、C、D的赞成票(对不起,我无法发布图片,所以我将其作为链接发送)。

Vote_table

但在这里,我只想把A1,A2,A3,A4加在一起;对于B,C仍然是单独计算的。我期望的输出是

Result_table

我试过的是

代码语言:javascript
复制
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

我也试过这种方法

代码语言:javascript
复制
if (type in ('A1', 'A2', 'A3', 'A4') and vote = 'yes' then count(*) else (if (vote = 'yes' then count(*)))) as cnt_yes

但这两种方法都不起作用。所以,我想请教一下这里的专家,有没有更好的方法呢?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-02 01:31:43

按计算类型分组:case when type in ('A1', 'A2', 'A3', 'A4') then 'A' else type end

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60477588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档