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

I would like to group all the values ​​that are negative and all positive, any ideas how to do this?

+5
source share
4 answers

GROUP BY SIGN (field) should work.

+11
source
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
+4
source

Stefan . , 0 ,

GROUP BY `field` >= 0
+2

- :

select count(*), IF(foo >= 0, "pos", "neg") as sign  from test group by sign;

foo -

EDIT: Stefan , , , .

0

All Articles