I'm trying to find the average for 4 returns a,b,c,d. Sometimes one return for ex a=0, I want to omit this one and only find the average of only a,b,c
S开发者_如何学PythonELECT SUM(CASE WHEN x > 0 THEN COALESCE(a, 0) + COALESCE(b, 0) + COALESCE(c, 0) + COALESCE(d, 0)) / 4
Please help!!!
Select Z.PrimaryKeyCol, Avg( Z.Value )
From (
Select PrimaryKeyCol, a As Value
Union All Select PrimaryKeyCol, b
Union All Select PrimaryKeyCol, c
Union All Select PrimaryKeyCol, d
) As Z
Where Z.Value <> 0
Group By Z.PrimaryKeyCol
select (a+b+c+d)
/
(if(a = 0,0,1) + if(b = 0,0,1) + if(c = 0,0,1) + if(d = 0,0,1) )
from table
精彩评论