arvind tegampure
arvind tegampure

Reputation: 33

Calculate Moving Average in SQL Server 2012

I need to calculate the moving average.

I am able to get this using Alteryx but not able to get the desired result using SQL.

Basically I have set values for period.

Value is available for 6 periods and I want forecast the next values using moving avg.

for example

Period     Value
01-04-2016 4
01-05-2016 5
01-06-2016 6

For Period 01-07-2016 it will be (4+5+6)/3 = 5

And next values

Period     Value
01-05-2016 5
01-06-2016 6
01-07-2016 5

For Period 01-08-2016 it will be (5+6+5)/3 = 5.33333 .

(6+5+5.333)/3 = 5.44444

(5+5.3333+5.4444) = 5.259259

and so on.

In table below 3MonthForecast is expected result.

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)

Upvotes: 1

Views: 202

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522254

We can try using AVG as an analytic function with an appropriate window taking the three preceding records:

SELECT 
    [Period],
    [Value],
    [3MonthForecast],
    AVG([3MonthForecast]) OVER (ORDER BY [Period] ROWS BETWEEN 3 PRECEDING AND 1 PRECEDING) AS MovingAvgForecast
FROM [dbo].[MovingAvg]
ORDER BY
    [Period];

Demo

Upvotes: 2

Related Questions