Hive - Unpivot functionality in hive

后端 未结 2 1172
鱼传尺愫
鱼传尺愫 2020-12-01 20:34

I have two table as follows:

Table A

userid | code | code_name | property_id
0001   | 1    | apple_id  | Y1234
0031   | 4    | mango_id  | G4567
0008         


        
2条回答
  •  不思量自难忘°
    2020-12-01 21:09

    Whenever I want to pivot a table in Hive, I collect key:value pairs to a map and then reference each key in the next level, creating new columns. This is the opposite of that.

    Query:

    select a.userid, y.new_id
    from (
      select new_id, fruit_name, fruit_code
      from (
        select new_id, map("apple_id", apple_id
                         , "mango_id", mango_id
                         , "grape_id", grape_id
                         , "peach_id", peach_id) as fruit_map
        from table_2 ) x
      lateral view explode(fruit_map) exptbl1 as fruit_name, fruit_code ) y
    join table_A a
    on (y.fruit_code=a.property_id)
    

    Output:

    0001    N456098
    0031    N002345
    0008    N129087
    00013   N109876
    

提交回复
热议问题