Is there a way to transpose data in Hive

点点圈 提交于 2019-12-18 11:57:27

问题


This is my table:

pid     high    medium    low
1       10      8         6
2       20      16        12
3       10      6         4

I want store this data in another table in Hive with the following format:

pid      priority      value
1         high          10 
1         medium        8
1         low           6
2         high          20
2         medium        16
2         low           12
3         high          10
3         medium        6
3         low           4

回答1:


Yes there is a way to do this in Hive. You just need to create a map and then explode said map.

Query:

CREATE TABLE db.new AS
SELECT pid, priority, value
FROM (
  SELECT pid
    , MAP('high', high, 'medium', medium, 'low', low) AS tmp
  FROM db.old ) x
LATERAL VIEW EXPLODE(tmp) explode_table AS priority, value

Output:

---------------------
pid  priority  value
---------------------
1    low       6
1    medium    8
1    high      10
2    low       12
2    medium    16
2    high      20
3    low       4
3    medium    6
3    high      10


来源:https://stackoverflow.com/questions/37436710/is-there-a-way-to-transpose-data-in-hive

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