shakedzy shakedzy - 3 months ago 7
SQL Question

SQL: Duplicate rows based on columns

I have a table that looks like this:

Name | Math | English | Arts
----------------------------------------
Brad | 87 | 65 | 90
Julie | 91 | 88 | 92


And I want to get:

Name | Grade
--------------
Brad | 87
Brad | 65
Brad | 90
Julie | 91
Julie | 88
Julie | 92


What's the simplest way to do that using SQL/Hive?

Answer

Something like this.

select name,math as Grade from your_table
union all
select name,English as Grade from your_table
union all
select name,Arts as Grade from your_table