Reputation: 339
I have a table with:
id integer
name text
sumbuy numeric
priority integer
....
table contains:
id , name , sumbuy, priority, ...
1 xx 33 12
2 dd 45 7
4 aa 54 0
I need to write a query that gives informatio based on priority
column.
The thing is that the data is changed based on priority.
Select case when priority>0 then 'High'::Text
when popartid<=0 then 'Low'::Text
end as Emg,*
from A
this yeilds:
Emg, id , name , sumbuy, priority, ...
High 1 xx 33 12
High 2 dd 45 7
Low 4 aa 54 0
But my goal is when the Emg
is Low
to have diffrent data which I give in manually. => name
will be 'not important' sumbuy
will be 0.
So what I want to see is:
Emg, id , name, sumbuy, priority, ...
High 1 xx 33 12
High 2 dd 45 7
Low 4 not important 0 0
basicly, What I am asking is if it's poosible for a select statment to perform diffrent instruction based on data in the row... something like:
Select case when priority>0
then 'High'::Text as Emg, id, name, sumbuy, priority
when popartid<=0
then 'Low'::citext as Emg, id, 'not important' as name, 0 as sumbuy, priority
from A
this isn't case
syntax but it demonstrat what I want.
How do I do that other than writing two diffrent queries?
Upvotes: 1
Views: 43
Reputation: 8103
You can do it by 2 ways I think of
1.
Select case when priority>0
then 'High'::Text
else 'Low'::citext end as Emg,
id,
case when priority>0 then name else 'not important' end as name,
case when priority>0 then sumbuy else 0 end as sumbuy
..
..
from A
2.
select 'High' as Emg,id,name,sumbuy,priority from A where priority>0
union all
select 'Low' as Emg,id,'not important' as name,0 as sumbuy,priority from A
where priority <=0
Upvotes: 2