我有一张桌子上有:
id integer
name text
sumbuy numeric
priority integer
....表中包括:
id , name , sumbuy, priority, ...
1 xx 33 12
2 dd 45 7
4 aa 54 0我需要编写一个基于priority列提供信息的查询。问题是数据是根据优先级进行更改的。
Select case when priority>0 then 'High'::Text
when popartid<=0 then 'Low'::Text
end as Emg,*
from A这些雪莲:
Emg, id , name , sumbuy, priority, ...
High 1 xx 33 12
High 2 dd 45 7
Low 4 aa 54 0但是我的目标是当Emg是Low时,有不同的数据,而我是手动提交的。=> name将是“不重要”,sumbuy将为0。所以我想看到的是:
Emg, id , name, sumbuy, priority, ...
High 1 xx 33 12
High 2 dd 45 7
Low 4 not important 0 0最基本的是,我要问的是,如果一个select状态根据行中的数据执行不同的指令是不可取的.类似于:
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这不是case语法,但它演示了我想要的。
除了编写两个不同的查询之外,我该如何做呢?
发布于 2015-10-18 09:18:27
你可以用我想到的两种方法来做
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 A2.
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 <=0https://stackoverflow.com/questions/33196149
复制相似问题