Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Bergulir standar deviasi di SQL Server

Apakah ini berhasil?

;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. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Gunakan NEWSEQUENTIALID() untuk Membuat GUID yang Bertambah di SQL Server

  2. Pemodelan data untuk tabel yang sama dengan kolom yang sama

  3. Catat perubahan catatan di server SQL dalam tabel audit

  4. Bagaimana melakukan CONTAINS() pada dua kolom SQL Pencarian Indeks Teks Lengkap

  5. Ubah float menjadi varchar di SQL Server tanpa notasi ilmiah