开发者

SQL Average of returns when there is one return=0

开发者 https://www.devze.com 2023-03-10 05:48 出处:网络
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

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号