Mistu4u
Mistu4u

Reputation: 5416

To get different row values in different columns in a single row

My table looks like this:

Param_id     Param_value
------------------------
A             1
B             2
C             3
D             4

.... and so on. Now I want only the values of Param_id "A" and "B".

Now I want to get the param_value in two different columns instead of two different rows. But if I use IN clause it will return the result in two rows. I want something like the below:

Param_value_1       Param_value_2
---------------------------------
1                   2

I can't use listagg or pivot because they are not serving my purpose. Is there any other way to achieve this? I searched in Google but could not find any solution for this.

Upvotes: 0

Views: 404

Answers (1)

user5683823
user5683823

Reputation:

The old way of pivoting... Since you are looking for the parameter values for parameter_id in ('A', 'B'), it doesn't make much sense to name the resulting columns param_value_1 and param_value_2; why not param_value_a and param_value_b? (Otherwise what determines that 'A' is 1 and 'B' is 2, and not the other way around?)

So - back to the old way of pivoting (although I suspect PIVOT will work too, regardless of requirement - unless you are on Oracle 10 or lower):

select max(case when param_id = 'A' then param_value end) as param_value_a,
       max(case when param_id = 'B' then param_value end) as param_value_b
from   your_table;

Upvotes: 1

Related Questions