Search code examples
sqlhivetranspose

Easy table transpose in hive


I need to transpose my table. Now i have that type of table:

Atr_1|Atr_2
 A   | 1
 A   | 2

But i want to get the next result

Atr_1|Atr_2|Atr_3
 A   | 1   |  2

How should i transpose my table for achieving this result?


Solution

  • Use case statements with min() or max() aggregation:

    select Atr_1,
           max(case when Atr_2=1 then 1 end ) Attr_2,
           max(case when Atr_2=2 then 2 end ) Attr_3
      from table t
     group by Atr_1;