SUM() all results (no group by clause) SUM() all results (no group by clause) sql sql

SUM() all results (no group by clause)


You can GROUP BY a constant on the table where you run the SELECT.

For example GROUP BY NULL:

mysql> select SUM(CASE WHEN product_id = 0 THEN -1 ELSE product_id END) AS sumprod, SUM(quantity) AS sumquant FROM orders GROUP BY NULL;+---------+----------+| sumprod | sumquant |+---------+----------+|       4 |        8 |+---------+----------+1 row in set (0.00 sec)mysql> select version();+-------------+| version()   |+-------------+| 5.5.25a-log |+-------------+1 row in set (0.00 sec)

But usually this error means that there is at least one column which is not grouped.

It might be that the production version of MySQL is taken aback by your use of a CASE within the aggregate function.