0

Say, I've a table as given below:

id, c1, c2, c3, ..... c365, StdDeviation
1 , 3 , 7 , 1 , ..... c365, null
2 , 4 , 5 , 2 , ..... c365, null
3 , 5 , 3 , 4 , ..... c365, null
4 , 6 , 2 , 6 , ..... c365, null
5
.
.
millions

I would like to get the following using MySQL

id, c1, c2, c3, ..... c365, StdDeviation
1 , 3 , 7 , 1 , ..... c365, *standard deviation of this row*
2 , 4 , 5 , 2 , ..... c365, *standard deviation of this row*
3 , 5 , 3 , 4 , ..... c365, *standard deviation of this row*
4 , 6 , 2 , 6 , ..... c365, *standard deviation of this row*
5
.
.
millions

I can't use STDDEV() as it do works vertically. Any solution?

4

1 回答 1

1

好吧,标准偏差是平均方差的平方根。你可以在这里阅读。

使用标准公式进行计算涉及大量算术。这是格式:

select sqrt((pow(c1 - cavg, 2) + pow(c2 - cavg, 2) + . . .
            ) / 365
           ) as stdev
from (select t.*,
             (c1 + c2 + c3 + . . . + c365)/365 as cavg
      from yourtable t
     ) t

这些公式很长。我建议您使用 Excel 之类的工具自动生成它们。

有替代的公式,但它们仍然涉及对值的平方求和并取值之和的平方——因此它不会简化公式的写出。

当数据以每个数据点一行的更规范化的格式存储时,表达公式要容易得多。这就是聚合函数的std()作用。

于 2013-07-07T19:07:39.537 回答