如何从多个列中查找值计数

时间:2019-11-04 04:10:58

标签: hive

有人可以帮助我从样本数据下方提取性别计数

Test

O / P可以是:

男18 F 11

男 18 11

1 个答案:

答案 0 :(得分:0)

select 'M',
       sum(case when col1='M' then 1 end)+
       sum(case when col2='M' then 1 end)+
       sum(case when col3='M' then 1 end)+
       sum(case when col4='M' then 1 end)+
       sum(case when col5='M' then 1 end) as m_cnt,
       'F',
       sum(case when col1='F' then 1 end)+
       sum(case when col2='F' then 1 end)+
       sum(case when col3='F' then 1 end)+
       sum(case when col4='F' then 1 end)+
       sum(case when col5='F' then 1 end) as f_cnt
  from your_table;