假设有一个包含3行的表:
date num
05-01 1
05-02 2
05-03 3
然后,使用新值计算其3天移动平均值。例如:
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
可以在SQL中实现吗?任何帮助将不胜感激。
答案 0 :(得分:0)
我会尝试创建一个包含30行的表,使用给定的值为前3个播种,并使用包含窗口函数的递归CTE来计算平均值,并使用INSERT语句将新行添加到表中。