gpt4 book ai didi

sql - 是否可以在同一个查询中执行不同的选择?

转载 作者:行者123 更新时间:2023-11-29 12:59:55 25 4
gpt4 key购买 nike

我有一张 table :

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

但我的目标是当 EmgLow 时,我会手动输入不同的数据。 => 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 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 语法,但它展示了我想要的。

除了编写两个不同的查询之外,我该怎么做?

最佳答案

你可以通过我想到的两种方式来实现

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

关于sql - 是否可以在同一个查询中执行不同的选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33196149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com