views:

13

answers:

2

I'm having trouble with sorting out a query for the following:

Data:
Column1  Column2
2        0
0        -2

I'm trying to select the difference between Column1 and Column2, with a minimum of 0. Eg.

Row1=2
Row2=0

My current query is SELECT (Column1 - Column2) as total FROM blah.

I tried adding max(Column2, 0) into the query, but that just errors.

+2  A: 

Try:

 SELECT GREATEST(Column1 - Column2, 0)
 from Table
Michael Pakhantsov
Thanks very much!
Blair McMillan
A: 

The MySQL function MAX() is an aggregate function, often used in combination with a GROUP BY. It only takes one argument (the name of the column of which you want to select the maximum value). The GREATEST() function is the function you need (as Michael Pakhantsov pointed out).

Lex