【发布时间】:2019-05-21 04:57:27
【问题描述】:
我需要计算移动平均线。
我可以使用 Alteryx 获得此结果,但无法使用 SQL 获得所需的结果。
基本上我已经为周期设置了值。
值可用于 6 个周期,我想使用移动平均值预测下一个值。
例如
Period Value
01-04-2016 4
01-05-2016 5
01-06-2016 6
对于 01-07-2016 期间,它将是 (4+5+6)/3 = 5
以及下一个值
Period Value
01-05-2016 5
01-06-2016 6
01-07-2016 5
对于 01-08-2016 期间,它将是 (5+6+5)/3 = 5.33333。
(6+5+5.333)/3 = 5.44444
(5+5.3333+5.4444) = 5.259259
等等。
下表中3个月的预测是预期结果。
CREATE TABLE [dbo].[MovingAvg]
(
[Period] [date] NULL,
[Value] [float] NULL,
[3MonthForecast] [float] NULL
)
INSERT [dbo].[MovingAvg] ([Period], [Value], [3MonthForecast]) VALUES (CAST(N'2016-01-01' AS Date), 1, 1)
GO
INSERT [dbo].[MovingAvg] ([Period], [Value], [3MonthForecast]) VALUES (CAST(N'2016-02-01' AS Date), 2, 2)
GO
INSERT [dbo].[MovingAvg] ([Period], [Value], [3MonthForecast]) VALUES (CAST(N'2016-03-01' AS Date), 3, 3)
GO
INSERT [dbo].[MovingAvg] ([Period], [Value], [3MonthForecast]) VALUES (CAST(N'2016-04-01' AS Date), 4, 4)
GO
INSERT [dbo].[MovingAvg] ([Period], [Value], [3MonthForecast]) VALUES (CAST(N'2016-05-01' AS Date), 5, 5)
GO
INSERT [dbo].[MovingAvg] ([Period], [Value], [3MonthForecast]) VALUES (CAST(N'2016-06-01' AS Date), 6, 6)
GO
INSERT [dbo].[MovingAvg] ([Period], [Value], [3MonthForecast]) VALUES (CAST(N'2016-07-01' AS Date), null, 5)
GO
INSERT [dbo].[MovingAvg] ([Period], [Value], [3MonthForecast]) VALUES (CAST(N'2016-08-01' AS Date), null, 5.333333333)
GO
INSERT [dbo].[MovingAvg] ([Period], [Value], [3MonthForecast]) VALUES (CAST(N'2016-09-01' AS Date), null, 5.444444444)
GO
INSERT [dbo].[MovingAvg] ([Period], [Value], [3MonthForecast]) VALUES (CAST(N'2016-10-01' AS Date), null, 5.259259259)
GO
INSERT [dbo].[MovingAvg] ([Period], [Value], [3MonthForecast]) VALUES (CAST(N'2016-11-01' AS Date), null, 5.345679012)
GO
INSERT [dbo].[MovingAvg] ([Period], [Value], [3MonthForecast]) VALUES (CAST(N'2016-12-01' AS Date), null, 5.349794239)
GO
INSERT [dbo].[MovingAvg] ([Period], [Value], [3MonthForecast]) VALUES (CAST(N'2017-01-01' AS Date), null, 5.31824417)
GO
INSERT [dbo].[MovingAvg] ([Period], [Value], [3MonthForecast]) VALUES (CAST(N'2017-02-01' AS Date), null, 5.337905807)
GO
INSERT [dbo].[MovingAvg] ([Period], [Value], [3MonthForecast]) VALUES (CAST(N'2017-03-01' AS Date), null, 5.335314739)
GO
INSERT [dbo].[MovingAvg] ([Period], [Value], [3MonthForecast]) VALUES (CAST(N'2017-04-01' AS Date), null, 5.330488239)
GO
INSERT [dbo].[MovingAvg] ([Period], [Value], [3MonthForecast]) VALUES (CAST(N'2017-05-01' AS Date), null, 5.334569595)
【问题讨论】:
-
请回答这个问题?
-
从
2016-07-01开始,[Value]列是NULL。为什么您会在 3 个月的预测中预期非NULL输出? -
嗨蒂姆,我有 6 个周期的值和我想使用移动平均预测的下一个值。所以基本上在我的表中我只有期间和值列。
标签: sql sql-server-2012 forecasting moving-average