Question

I have a table, two columns some rows:

c1 | c2

--------

10 | 90

11 | 89.5

12 | 89

13 | 87

14 | null

15 | 86

I want to get the MAX value of column c2 but only between c1's values 12 and 15
I try with:

SELECT MAX(IFNULL(c2, 0)) AS max_value FROM mytable WHERE data BETWEEN 12 AND 15

but not working. Is there a way to ignore the null value ?

Was it helpful?

Solution

MAX aggregated function will automatically skip null values, so the following query should be fine:

SELECT MAX(c2) FROM tablename WHERE c1 BETWEEN 12 AND 15;

Please see fiddle here. I see your where condition is

WHERE data BETWEEN 12 AND 15

are you sure you are applying the WHERE clause to the correct column? I think you need to change it to:

WHERE c1 BETWEEN 12 AND 15

OTHER TIPS

"The max() aggregate function returns the maximum value of all values in the group. The maximum value is the value that would be returned last in an ORDER BY on the same column. Aggregate max() returns NULL if and only if there are no non-NULL values in the group." - http://sqlite.org/lang_aggfunc.html

Assumed, you have no negatives and you don't want NULL to be 0, You can simply use

SELECT MAX(c2) AS max_value FROM mytable ...

If all rows for c2 are null, max(c2) will return null

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top