Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql, how can I group by, in a select query on positve or negative?

I would like to group by all the the values which are negative and all those which are positive, any ideas how to do this ?

like image 395
Jules Avatar asked Jan 25 '11 17:01

Jules


2 Answers

GROUP BY SIGN(field) should work.

like image 55
Stefan H Singer Avatar answered Nov 04 '22 23:11

Stefan H Singer


SELECT SUM(CASE WHEN SomeColumn < 0 THEN 1 ELSE 0 END) AS negative_values,
       SUM(CASE WHEN SomeColumn >=0 THEN 1 ELSE 0 END) AS non_negative_values
    FROM YourTable
like image 21
Joe Stefanelli Avatar answered Nov 04 '22 23:11

Joe Stefanelli