Hacker
Hacker

Reputation: 7906

Having multiple columns under 1 if clause

I am trying to write query like below

select 

if drophip = 1 then
0 as a,
0 as b,
1 as c,
1 as d

else if drophip = 0 then
1 as a,
1 as b,
0 as c,
0 as d

end if; 

from tabl1;

its giving syntax error. Is there any way i can write the same?

Upvotes: 1

Views: 45

Answers (1)

Joe Taras
Joe Taras

Reputation: 15389

Try this:

I suppose your drophip is boolean field.

Query

select 
case
    when drophip = 1 then 0
    else 1
end as a,
case
    when drophip = 1 then 0
    else 1
end as b,
case
    when drophip = 1 then 1
    else 0
end as c,
case
    when drophip = 1 then 1
    else 0
end as d
from tabl1;

Upvotes: 3

Related Questions