mysql - How to write this query have 4 quarters and value is negative -


table_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            

from table_a, want symbols quarter has q1 , q2 , q3 , q4 value negative.

so result should s1 , s3.

select symbol  table_a  quarter='a' ,        quarter='b' ,        quarter='c' ,        quarter='d' , value<0  group symbol 

you can use conditional aggregation this:

select symbol table_a value < 0 group symbol having max(case when quarter = 'q1' 1 else 0 end) = 1    , max(case when quarter = 'q2' 1 else 0 end) = 1    , max(case when quarter = 'q3' 1 else 0 end) = 1    , max(case when quarter = 'q4' 1 else 0 end) = 1