0

Assume there is a table comprises 3 rows:

date      num
05-01      1
05-02      2
05-03      3

Then,calculate its 3-day moving average using new values. For example:

1st result = (1+2+3)/3=2
2nd result = (2+3+2)/3=2.33
3rd result = (3+2+2.33)/3=2.43
4th result = (2+2.33+2.43)/3=2.25
5th result = (2.33+2.43+2.25)/3=2.34

....till the 30th result

Is is possible to implement this in SQL? Any help will be appreciated.

superz
  • 99
  • 1
  • 2
  • 10

1 Answers1

0

I would try creating a table with 30 rows, seeding the first 3 with your given values, and the use a recursive CTE that incorporates a window function to calculate the average and an INSERT statement to add the new row to the table.

rd_nielsen
  • 2,407
  • 2
  • 11
  • 18