I want to query the order table and show the customer ID and the total amount of all his orders, however, orders can have positive or negative totals.
select customer_id, SUM(order_total) from orders group by customer_id;
Now my question is: how can I do the following in a single sql query:
If the total amount is positive, I want to display it as is; if the total amount is negative, I just want to display 0 instead of the actual amount.
What I'm looking for is a function that can handle this, like the IFNULL function ( IFNULL(SUM(order_total),0) ), but instead of checking for null, it should check for a negative result.
Pseudocode:
IFNEGATIVE(SUM(order_total),0)
Is there an easy way in standard sql (or, in particular, in Mysql 5.5, it will be good too).
source share