SQL: AVG with NULL Values

user3364656 picture user3364656 · Mar 6, 2014 · Viewed 35.8k times · Source

as far as i understood the AVG() function ignores NULL Values.

So AVG(4,4,4,4,4,NULL) --> 4

In my case i don´t want this to happen.

I need a solution like that: AVG(4,4,4,4,4,NULL) --> 3,33

without replacing the NULL values directly in the table itself. Is there any way to do this?

Answer

Bohemian picture Bohemian · Mar 6, 2014

Use coalesce() to return the real value of zero for null columns:

select avg(coalesce(some_column, 0))
from ...