mysql - AVG of Values except 0 (except 0 is only value) in SQL -
i'd have avg excluding 0s list of values can contain them, except in case 0 values available (or @ least there's 1 row , values 0).
at moment use
select avg (nullif(feature, 0)) cluster_density_feat cluster_id=1;
but returns null if feature value cluster_id has 1 row , feature values 0 or if every row of cluster_id has 0 values.
how can achieve this?
avg()
uses values group, cannot exclude 0
s. should exclude them using where
clause.
select ifnull(avg(feature), 0) cluster_density_feat cluster_id = 1 , feature <> 0
if values in feature
column 0
, feature <> 0
condition produces empty set of rows , avg()
returns null
. function ifnull()
returns second argument when first 1 null
.
Comments
Post a Comment