Reputation: 5
I have one table , and I want to select records depend on the value of one column in the same table. Suppose I have table of cars, one column is having the records of car type, and when I select car type(1) I want to show record of the color, and if I select car type(2) I want to show record of the model....etc.
I tried to use CASE, but I don't know how to use it properly for this condition.
Example:
select T1.column1,T1.column2, T2.column 3,T2.column10
case
when T2.column10='ABC' then select T2.column6,T2.column7
when T2.column10='DEF' then select T2.column8,T2.column9
when T2.column10='GHI' then select T2.column11,T2.column12
End
from table1 T1,table2 T2
where T1.column1=T2.column1
Upvotes: 0
Views: 69
Reputation: 164139
Each branch of the CASE
expression must return only 1 value, so you have to repeat it twice to get the 2 columns:
select T1.column1,T1.column2, T2.column3,T2.column10,
case T2.column10
when 'ABC' then T2.column6
when 'DEF' then T2.column8
when 'GHI' then T2.column11
end columnX,
case T2.column10
when 'ABC' then T2.column7
when 'DEF' then T2.column9
when 'GHI' then T2.column12
end columnY
from table1 T1 inner join table2 T2
on T1.column1=T2.column1
I also change your old style join to a proper join.
Upvotes: 1