假设我有一个包含5列的表,
A,B,C,D,E
是整数。我希望得到那个案例中所有字段的平均值,而不是3。
所以,在一些样本数据上:
A, B, C, D, E DESIRED RESULT
-------------
1, 1, 4, 4, 3 -> 2.5 (NOT 2.6)
1, 2, 3, 3, 3 -> 1.5 (NOT 2.4)
编辑:我找到了解决方案。
(
(
IF(A!=3,A,0)
+IF(B!=3,B,0)
+IF(C!=3,C,0)
+IF(D!=3,D,0)
+IF(E!=3,E,0)
)
/
(
IF(A!=3,1,0)
+IF(B!=3,1,0)
+IF(C!=3,1,0)
+IF(D!=3,1,0)
+IF(E!=3,1,0)
)
) as VALUE
答案 0 :(得分:1)
试试:
select avg(CASE WHEN A = 3 then 0 else A end +
CASE WHEN B = 3 then 0 else B end +
CASE WHEN C = 3 then 0 else C end +
CASE WHEN D = 3 then 0 else D end +
CASE WHEN E = 3 then 0 else E end)/
(CASE WHEN A = 3 then 0 else 1 end +
CASE WHEN B = 3 then 0 else 1 end +
CASE WHEN C = 3 then 0 else 1 end +
CASE WHEN D = 3 then 0 else 1 end +
CASE WHEN E = 3 then 0 else 1 end) as av from table1
group by A,B,C,D,E