Hive 蜂巢枢轴和计数

Hive 蜂巢枢轴和计数,hive,count,pivot,transpose,Hive,Count,Pivot,Transpose,我有一张表,我正试图找出如何根据这些数据进行数据透视和计数。 这个例子可能不太合适,但结果正是我想要的 输入示例: name |chinese|math|english tom |A |A |B tom |B |A |C peter|B |C |C peter|A |B |C 示例输出: name |object |A|B|C tom |chinese|1|1|0 tom |math |2|0|0 tom |english|

我有一张表,我正试图找出如何根据这些数据进行数据透视和计数。 这个例子可能不太合适,但结果正是我想要的

输入示例:

name |chinese|math|english
tom  |A      |A   |B
tom  |B      |A   |C
peter|B      |C   |C
peter|A      |B   |C
示例输出:

name |object |A|B|C
tom  |chinese|1|1|0
tom  |math   |2|0|0
tom  |english|0|1|1
peter|chinese|1|1|0
peter|math   |0|1|1
peter|english|0|0|2

将UNIONALL与聚合一起使用

演示:

with your_table as (
select stack(4,
'tom'  ,'A','A','B',
'tom'  ,'B','A','C',
'peter','B','C','C',
'peter','A','B','C'
) as (name,chinese,math,english)
)

select name, 'chinese' as object, 
       count(case when chinese='A' then 1 end) as A, 
       count(case when chinese='B' then 1 end) as B,
       count(case when chinese='C' then 1 end) as C
  from your_table 
 group by name
    UNION ALL 
select name, 'math' as object, 
       count(case when math='A' then 1 end) as A, 
       count(case when math='B' then 1 end) as B,
       count(case when math='C' then 1 end) as C
  from your_table 
 group by name
    UNION ALL 
select name, 'english' as object, 
       count(case when english='A' then 1 end) as A, 
       count(case when english='B' then 1 end) as B,
       count(case when english='C' then 1 end) as C
  from your_table 
 group by name;
结果:

name    object  a       b       c
peter   chinese 1       1       0
tom     chinese 1       1       0
peter   math    0       1       1
tom     math    2       0       0
peter   english 0       0       2
tom     english 0       1       1

将UNIONALL与聚合一起使用

演示:

with your_table as (
select stack(4,
'tom'  ,'A','A','B',
'tom'  ,'B','A','C',
'peter','B','C','C',
'peter','A','B','C'
) as (name,chinese,math,english)
)

select name, 'chinese' as object, 
       count(case when chinese='A' then 1 end) as A, 
       count(case when chinese='B' then 1 end) as B,
       count(case when chinese='C' then 1 end) as C
  from your_table 
 group by name
    UNION ALL 
select name, 'math' as object, 
       count(case when math='A' then 1 end) as A, 
       count(case when math='B' then 1 end) as B,
       count(case when math='C' then 1 end) as C
  from your_table 
 group by name
    UNION ALL 
select name, 'english' as object, 
       count(case when english='A' then 1 end) as A, 
       count(case when english='B' then 1 end) as B,
       count(case when english='C' then 1 end) as C
  from your_table 
 group by name;
结果:

name    object  a       b       c
peter   chinese 1       1       0
tom     chinese 1       1       0
peter   math    0       1       1
tom     math    2       0       0
peter   english 0       0       2
tom     english 0       1       1

谢谢你的回答,这是有效的。但是我想用其他的方式,比如str_to_map,collect_list这种,我该怎么做?@cheryH这个任务你不需要str_to_map,也不需要collect_list谢谢你的回答,这是有效的。但是我想用str_to_map,collect_list这种方式,我该怎么做?@cheryH对于这项任务,您不需要str_to_map,也不需要收集_list