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 0s. 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

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -