如何写此查询有4个季度且值是负数

测试仪

表_a

SymbolQuarter  Value       
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,我想获取四分之一的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  

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章