Transpose dataset in Hive

我的未来我决定 提交于 2020-01-25 07:58:48

问题


I'm trying to transpose a variable in Hive such as:

Id1  Id2 Event
 1    1   7
 2    2   3
 2    2   7

to

 Id1  Id2 Event_7 Event_3
  1    1   1
  2    2   1        1

Following is what I have so far:

 create temporary table event_trans as 
           select Id1, Id2,Event
           kv['3'] as Event_3,
           kv['7'] as Event_7
           from(
             select Id1, Id2, collect(Event, '1') as kv
             from event1
             group by Id1, Id2

             )t

Error: Error while compiling statement: FAILED: ParseException line 1:84 missing EOF at '[' near 'kv'

I'm also interested to know how to transpose a dataset in Hive with duplicates such as to the same output:

Id1  Id2 Event
 1    1   7
 2    2   3
 2    2   7
 2    2   7

to

 Id1  Id2 Event_7 Event_3
  1    1   1
  2    2   1        1

Appreciate for any help!


回答1:


In Hive SQL, you can do conditional aggregation:

select 
    id1,
    id2,
    max(case when event = 7 then 1 end) event_7,
    max(case when event = 3 then 1 end) event_3
group by id1, id2
order by id1, id2


来源:https://stackoverflow.com/questions/59740785/transpose-dataset-in-hive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!