CodeIgniter Forums

Full Version: Calculate average, ignore zero ('0')?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]mvdg27[/eluser]
Hey guys,

not directly CI related .. but does anyone know how to calculate the average of a column, but ignoring the zero values?

So imagine: 10, 8, 6, 0, 4 => average = 28 / 5 = 5.6

Instead I would like: 10, 8, 6, 0 (ignore this value!), 4 => average = 28 / 4 = 7

I'm using MySql.

Thanks, Michiel

El Forum

[eluser]Dam1an[/eluser]
This should do it
just select anything greater then 0 for the AVG
Code:
SELECT AVG(`number`) FROM test WHERE `number` > 0

El Forum

[eluser]renownedmedia[/eluser]
Maybe use NULL's instead of 0's