2

I have a table in Hive in below format.

std_id  std_name sub_id sub_name marks 
1       xxx      123    Eng      70
1       xxx      125    Maths    90
1       xxx      124    Science  80 

I want a table in below format as output.

std_id std_name Eng Science Maths  
1      xxx      70  80      90

how can i get the output with using Hive Query..

Vamsi Prabhala
  • 48,685
  • 4
  • 36
  • 58
HEMANT PATEL
  • 77
  • 1
  • 11
  • Possible duplicate of [How to transpose/pivot data in hive?](https://stackoverflow.com/questions/23025380/how-to-transpose-pivot-data-in-hive) – Staza Mar 18 '18 at 08:56

1 Answers1

1

Use MAX ( CASE.. ) with GROUP BY

SELECT std_id
    ,std_name
    ,MAX(CASE 
            WHEN sub_name = 'Eng'
                THEN marks
            END) AS Eng
    ,MAX(CASE 
            WHEN sub_name = 'Science'
                THEN marks
            END) AS Science
    ,MAX(CASE 
            WHEN sub_name = 'Maths'
                THEN marks
            END) AS Maths
FROM t
GROUP BY std_id
    ,std_name;
Kaushik Nayak
  • 30,772
  • 5
  • 32
  • 45