2013-07-07 21 views
0

說,我有一個表,如下所示:MySql:如何獲得每行的標準偏差?

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 

我想獲得使用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 

我不能使用STDDEV(),因爲它做的工作垂直以下。任何解決方案

+0

是的,我確實,對不起,以前的問題不符合我的需要。其實,我正在計算相關性。但試圖學會首先得到標準偏差:) –

+1

看看這個問題:http://stackoverflow.com/questions/3985333/how-to-select-standard-deviation-within-a-row-in-sql - 或 - r –

+0

是的,我已經看到了,我已經搜索了很多之前,我發佈了這個問題。那麼,在該鏈接中,解決方案是創建一個單獨的表。有沒有辦法創建另一個表? –

回答

1

那麼,標準差就是平均方差的平方根。你可以閱讀關於它here

使用標準公式計算它需要大量的算術運算。這裏是格式:

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()所做的。