Mysql AVG忽略零

扫码查看
本文介绍了Mysql AVG忽略零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对一列执行平均,但我知道该列中的大多数值将为零.在所有可能的行中,只有两个可能具有正值.我怎么能告诉mySQL忽略零,而只取平均值呢?

I need to perform an avg on a column, but I know that most of the values in that column will be zero. Out of all possible rows, only two will probably have positive values. How can I tell mySQL to ignore the zeros and only average the actual values?

推荐答案

假设您可能不想完全排除此类行(也许它们在要聚合的其他列中有值)

Assuming that you might want to not totally exclude such rows (perhaps they have values in other columns you want to aggregate)

SELECT AVG(NULLIF(field ,0))
from table

这篇关于Mysql AVG忽略零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:36
查看更多