SQL Server中的滚动标准偏差

时间:2012-01-27 14:11:36

标签: sql sql-server sql-server-2008

我有按对象ID分组的时间序列数据。我想知道如何在滚动的基础上计算今天和4天前PER OBJECT之间所有值的标准差。即:

 id      date        value                   stdev
12345   24/11/2005  0.0001923076923076920   0.0000000000000000000000
37470   25/11/2005  0.0001923076923076920   0.0000000000000000000000
37470   28/11/2005  0.0001923076923076920   0.0000000000000000000000
37470   29/11/2005  0.0001923076923076920   0.0000000000000000000000
37470   30/11/2005  0.0001923076923076920   0.0000000000000000000000
37470   01/12/2005  0.0001923076923076920   0.0000000000000000000000
37470   02/12/2005  0.0001921229586935640   0.0000000826153837838936
37470   05/12/2005  0.0001923076923076920   0.0000000826153837838936
37470   06/12/2005  0.0001923076923076920   0.0000000826153837838937

67890   07/12/2005  0.0001923076923076920   0.0000000000000000000000
67890   08/12/2005  0.0001923076923076920   0.0000000000000000000000
67890   09/12/2005  0.0001923076923076920   0.0000000000000000000000
67890   12/12/2005  0.0001923076923076920   0.0000000000000000000000
67890   13/12/2005  0.0001923076923076920   0.0000000000000000000000
67890   14/12/2005  0.0001923076923076920   0.0000000000000000000000
67890   15/12/2005  0.0001923076923076920   0.0000000000000000000000
67890   16/12/2005  0.0001923076923076920   0.0000000000000000000000
67890   19/12/2005  0.0001923076923076920   0.0000000000000000000000
67890   20/12/2005  0.0001923076923076920   0.0000000000000000000000
67890   21/12/2005  0.0001923076923076920   0.0000000000000000000000
67890   22/12/2005  0.0001923076923076920   0.0000000000000000000000
67890   23/12/2005  0.0001923076923076920   0.0000000000000000000000
67890   26/12/2005  0.0001923076923076920   0.0000000000000000000000
67890   27/12/2005  0.0001923076923076920   0.0000000000000000000000
67890   28/12/2005  0.0001923076923076920   0.0000000000000000000000
67890   29/12/2005  0.0001923076923076920   0.0000000000000000000000
67890   30/12/2005  0.0001923076923076920   0.0000000000000000000000
67890   02/01/2006  0.0001922984471900390   0.0000000041345423064249

1 个答案:

答案 0 :(得分:0)

这可以解决这个问题吗?

;WITH sample_data AS
(
SELECT 12345 as id,  '2005-11-24' as date, 0.0001923076923076920 as datas 
UNION ALL SELECT 37470 ,  '2005-11-25' , 0.0001923076923076920 
UNION ALL SELECT 37470 ,  '2005-11-28' , 0.0001923076923076920 
UNION ALL SELECT 37470 ,  '2005-11-29' , 0.0001923076923076920 
UNION ALL SELECT 37470 ,  '2005-11-30' , 0.0001923076923076920 
UNION ALL SELECT 37470 ,  '2005-12-01' , 0.0001923076923076920 
UNION ALL SELECT 37470 ,  '2005-12-02' , 0.0001921229586935640 
UNION ALL SELECT 37470 ,  '2005-12-05' , 0.0001923076923076920 
UNION ALL SELECT 37470 ,  '2005-12-06' , 0.0001923076923076920 
)
,sample_data2 AS
(
SELECT 
id
,date
,datas
,row_number() OVER (PARTITION BY id ORDER BY date ASC) as row
FROM sample_data
)
,sample_data3 AS
(
SELECT  
sd2.id AS id
,sd2.date as Date
,sd2.datas as orig_data
,sd3.datas as prior_data
FROM sample_data2 sd2
LEFT OUTER JOIN sample_data2 sd3 ON sd3.id = sd2.id
AND sd3.row <= sd2.row
AND sd3.row > sd2.row - 5
AND sd2.row > 0
)
SELECT
 id
,Date
,MAX(orig_data) as Data_value
,ISNULL(STDEV(prior_data),0) AS STDEV_
FROM sample_data3
GROUP BY
id 
,date