CodeIgniter Forums
Calculate average, ignore zero ('0')? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Calculate average, ignore zero ('0')? (/showthread.php?tid=21830)



Calculate average, ignore zero ('0')? - El Forum - 08-21-2009

[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


Calculate average, ignore zero ('0')? - El Forum - 08-21-2009

[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



Calculate average, ignore zero ('0')? - El Forum - 08-21-2009

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