question

Gogolo avatar image
Gogolo asked

Select results format

Dears, How can I make a query do display results in vertical way, for example: SELECT 1 AS Device, 2 AS ACTIVE, 3 AS passive the results will be displayed in the way below: Device ACTIVE passive 1 2 3 and I need results in a way below Device 1 ACTIVE 2 passive 3 thank you in advanced.
sql-server-2008
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image
Kev Riley answered
I'm sure the real requirement is much more complex, however you can use a `union`: select 'Device' as col1, Device as col2 from (SELECT 1 AS Device, 2 AS ACTIVE, 3 AS passive) YourTable union all select 'ACTIVE', ACTIVE from (SELECT 1 AS Device, 2 AS ACTIVE, 3 AS passive) YourTable union all select 'passive', passive from (SELECT 1 AS Device, 2 AS ACTIVE, 3 AS passive) YourTable gives col1 col2 ------- ----------- Device 1 ACTIVE 2 passive 3 (3 row(s) affected)
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Beandon10 avatar image
Beandon10 answered
I was able to get the desired results with the following query: SELECT * FROM (VALUES ('Device', 1), ('ACTIVE', 2), ('passive', 3)) AS Tab(Col1, Col2)
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.