Created
March 15, 2019 13:33
-
-
Save johanrex/473c4e35c3ee9705b47f380d2076889c to your computer and use it in GitHub Desktop.
SQL moving average
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH cte | |
AS (SELECT | |
1 AS n -- anchor member | |
,Cast(Cast(CRYPT_GEN_RANDOM(4) as INT) as Float) AS rnd | |
UNION ALL | |
SELECT | |
n + 1 -- recursive member | |
,Cast(Cast(CRYPT_GEN_RANDOM(4) as INT) as Float) | |
FROM cte | |
WHERE n < 50 -- terminator | |
) | |
SELECT | |
n | |
,rnd | |
,AVG(rnd) OVER(ORDER BY n ASC ROWS 2 PRECEDING) AS MA3 | |
FROM cte; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment