sql >> Databasteknik >  >> RDS >> Sqlserver

Rullande standardavvikelse i SQL Server

Gör detta susen?

;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



  1. ER_TRUNCATED_WRONG_VALUE:Felaktigt datetime-värde

  2. Entitetsramverk PostgreSQL

  3. SQL-kommandot avslutades inte korrekt Undantag

  4. Hur man skapar schema i Oracle med SQL Developer?