Srinivas Srinivas - 3 months ago 6
SQL Question

Select SQL Columns as added rows

I have a SQL Server 2008 table as below...

ID Others1Desc Others1Value Others2Desc Others2value
-- ----------- ------------ ----------- ------------
id_x xyz 12.50 pqr 10.00
id_y abc NULL mno 1.05


Now, I want the select result as below...

ID ItemDesc ItemValue
-- ----------- ------------
id_x xyz 12.50
id_x pqr 10.00
id_y abc NULL
id_y mno 1.05


Any guidance is highly appreciated. Thanks

Answer

This way:

select ID, Others1Desc as ItemDesc,Others1Value as itemValue from
(select ID ,   Others1Desc ,   Others1Value from table1

Union all

select ID ,   Others2Desc,   Others2value from table1)as a
Comments