Group varying number of rows as columns in Hive table

一曲冷凌霜 提交于 2019-12-11 11:25:57

问题


I have a Hive table that contains userIDs and some variable choice, and basically looks like this:

userID    selection
   1          A
   1          D
   1          F
   2          A
   2          C

What I would like to do is condense this information and end up with something like:

 userID    selection1    selection2    selection3
    1          A             D              F
    2          A             C

Is this even possible? It isn't clear to me how to do this grouping, given that the number of possible selections varies with the user.

It would even be fine if I could do something like:

 userID    selection 
    1        A,D,F    
    2         A,C     

I have tried several approaches but so far nothing has been close enough to describe. I think what I want is something of the form:

select userID, group_concat(selection) from table_name group by userID

but as far as I can tell the group_concat function isn't available.

Thanks!


回答1:


In case anyone ends up needing the answer to this, it can be achieved via:

select userID, collect_set(selection) from table_name group by userID


来源:https://stackoverflow.com/questions/31441657/group-varying-number-of-rows-as-columns-in-hive-table

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