0

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

SDE
  • 1
  • 1
  • 5

1 Answers1

1

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.

Gouda Elalfy
  • 6,888
  • 1
  • 26
  • 38
  • It's okay. But i need it by using only SELECT statement and WHERE clause without using UNION/UNION ALL. – SDE Jan 06 '16 at 11:39
  • i am fine with the code, but my requirement is like that.Could you please provide me the solution with my requirement. – SDE Jan 06 '16 at 12:34