bircastri
bircastri

Reputation: 2167

How to create a query that show the result into column and not in rows

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?

Upvotes: 0

Views: 36

Answers (1)

Ullas
Ullas

Reputation: 11556

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];

Upvotes: 1

Related Questions