0

I want to get a Query to select some row from a table but I want to show the results not on rows but on column.

So for example:

SELECT V.IDPARAMETER AS ID, V.VALUE AS VALUE
FROM AA_V_RESULTS V
WHERE V.ID = ?

The result of this query is

ID  VALUE
150 177
151 IN THE NORM
152  OK

Now I want that the result is this:

if(id = 150)
   name of column is 'PARAMETER1'
else if(id = 151)
   name of column is 'PARAMETER2'
else if(id = 152)
   name of column is 'PARAMETER3'

So the result of the query is

PARAMETER1 PARAMETER2 PARAMETER3
177        IN THE NORM OK

It is possible to do this?

bircastri
  • 2,169
  • 13
  • 50
  • 119
  • Possible duplicate of [SQL Server dynamic PIVOT query?](https://stackoverflow.com/questions/10404348/sql-server-dynamic-pivot-query) – iamdave Oct 04 '17 at 13:24

1 Answers1

1

You can use a CASE expression.

Query

select 
  max(case [ID] when 150 then [VALUE] end) as [Parameter1],
  max(case [ID] when 151 then [VALUE] end) as [Parameter2],
  max(case [ID] when 152 then [VALUE] end) as [Parameter3]
from [your_table_name];
Ullas
  • 11,450
  • 4
  • 33
  • 50