SQL:具有NULL值的AVG [英] SQL: AVG with NULL Values
本文介绍了SQL:具有NULL值的AVG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
AVG()函数忽略NULL值.
as far as i understood the AVG() function ignores NULL Values.
所以AVG(4,4,4,4,4,NULL)-> 4
So AVG(4,4,4,4,4,NULL) --> 4
就我而言,我不希望这种情况发生.
In my case i don´t want this to happen.
我需要一个这样的解决方案:AVG(4,4,4,4,4,4,NULL)-> 3,33
I need a solution like that: AVG(4,4,4,4,4,NULL) --> 3,33
,而无需直接在表本身中替换NULL值. 有什么办法吗?
without replacing the NULL values directly in the table itself. Is there any way to do this?
推荐答案
使用coalesce()
为空列返回零的实值:
Use coalesce()
to return the real value of zero for null columns:
select avg(coalesce(some_column, 0))
from ...
这篇关于SQL:具有NULL值的AVG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文