gpt4 book ai didi

mysql - 如何编写此查询有 4 个季度且值为负

转载 作者:行者123 更新时间:2023-11-29 01:55:43 24 4
gpt4 key购买 nike

表_a

符号季度
S1 Q1 -1
S1 Q2 -1
S1 Q3 -1
S1 Q4 -1
S2 Q1 -1
S2 Q3 -1
S3 Q1 -1
S3 Q3 -1
S3 Q4 -1
S3 Q2 -1

我想从 Table_a 中获取 Quarter 的 Q1 和 Q2 以及 Q3 和 Q4 值为负的符号。

所以结果应该是S1和S3。

select symbol 
from Table_a
where Quarter='A' and
Quarter='B' and
Quarter='C' and
Quarter='D' and Value<0
group by symbol

最佳答案

您可以为此使用条件聚合:

select symbol
from Table_a
where value < 0
group by symbol
having max(case when quarter = 'Q1' then 1 else 0 end) = 1
and max(case when quarter = 'Q2' then 1 else 0 end) = 1
and max(case when quarter = 'Q3' then 1 else 0 end) = 1
and max(case when quarter = 'Q4' then 1 else 0 end) = 1

关于mysql - 如何编写此查询有 4 个季度且值为负,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29909200/

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