I have a table like this
Name Preference_1 Preference_2 Preference_3
John India UK USA
Output should be like this
Name Preference_No Location
John 1 India
John 2 UK
John 3 US
I have a table like this
Name Preference_1 Preference_2 Preference_3
John India UK USA
Output should be like this
Name Preference_No Location
John 1 India
John 2 UK
John 3 US
you can do this using union
or union all
like this:
select Name, '1' AS Preference_No, Preference_1 AS Location from your_table union
select Name, '2' AS Preference_No, Preference_2 AS Location from your_table union
select Name, '3' AS Preference_No, Preference_3 AS Location from your_table union
order by Name
Note: you can use union all
not union
to get all records otherwise what is repeated, but in your case you don't need to use union all
because you are select static different values As Preference_No, so there is no records will repeated.