Logo

SQL Server MovingFORECAST Function

Updated 2023-11-13 21:15:15.150000

Description

Use the scalar function MovingFORECAST to calculate the predicted value of y for a specific value of x for a series of x- and y-values within a resultant table or partition, without the need for a self-join. The intercept value is calculated for each value from the first value in the window to the last value in the window. If the column values are presented to the functions out of order, an error message will be generated.

Syntax

SELECT [westclintech].[wct].[MovingFORECAST](
  <@New_x, float,>
 ,<@Y, float,>
 ,<@X, float,>
 ,<@Offset, int,>
 ,<@RowNum, int,>
 ,<@Id, tinyint,>)

Arguments

@New_x

the specific x-value used to forecast the y-value. @New_x is an expression of type float or of a type that can be implicitly converted to float.

@Y

the y-value passed into the function. @Y is an expression of type float or of a type that can be implicitly converted to float.

@X

the x-value passed into the function. @X is an expression of type float or of a type that can be implicitly converted to float.

@Offset

specifies the window size. @Offset is an expression of type int or of a type that can be implicitly converted to int.

@RowNum

the number of the row within the group for which the sum is being calculated. If @RowNum for the current row in a set is less than or equal to the previous @RowNum and @RowNum is not equal to 1, an error message will be generated. @RowNum is an expression of type int or of a type that can be implicitly converted to int.

@Id

a unique identifier for the MovingFORECAST calculation. @Id allows you to specify multiple MovingFORECAST calculations within a resultant table. @Id is an expression of type tinyint or of a type that can be implicitly converted to tinyint.

Return Type

float

Remarks

If @Id is NULL then @Id = 0.

@RowNum must be in ascending order.

To calculate the forecast from the first row of a dataset or of a partition of x- and y-values use the RunningFORECAST function.

If @RowNum = 1 then MovingFORECAST is NULL.

To calculate a single forecast value for a new x-value and a set of x- and y-values use the FORECAST function.

Set @X to NULL to have the function maintain a constant set of x-values in the range 1 to window-size.

There may be cases where the order in which the data are returned to the function and the order in which the results are returned are different, generally due to parallelism. You can use OPTION(MAXDOP 1) or OPTION(MAXDOP 1,FORCE ORDER) to help eliminate this problem.

Examples

In this example we will store the monthly sales for three products: Leaf Blowers, Snow Blowers, and Pool Supplies. We will use the MovingFORECAST function to predict the new month's sales based on the preceding 6 months historical sales. We will use the RANK() function to number the months, with the earliest month being assigned a rank of 1.

--Create the temporary table
CREATE TABLE #f
(
    EOM datetime,
    item varchar(20),
    sales money
);
--Populate the table with some data
INSERT INTO #f
VALUES
('2010-10-31', 'Leaf Blowers', 42548);
INSERT INTO #f
VALUES
('2010-11-30', 'Leaf Blowers', 77227);
INSERT INTO #f
VALUES
('2010-12-31', 'Leaf Blowers', 66944);
INSERT INTO #f
VALUES
('2011-01-31', 'Leaf Blowers', 34591);
INSERT INTO #f
VALUES
('2011-02-28', 'Leaf Blowers', 73468);
INSERT INTO #f
VALUES
('2011-03-31', 'Leaf Blowers', 50102);
INSERT INTO #f
VALUES
('2011-04-30', 'Leaf Blowers', 87270);
INSERT INTO #f
VALUES
('2011-05-31', 'Leaf Blowers', 51555);
INSERT INTO #f
VALUES
('2011-06-30', 'Leaf Blowers', 75139);
INSERT INTO #f
VALUES
('2011-07-31', 'Leaf Blowers', 50682);
INSERT INTO #f
VALUES
('2011-08-31', 'Leaf Blowers', 96577);
INSERT INTO #f
VALUES
('2011-09-30', 'Leaf Blowers', 77553);
INSERT INTO #f
VALUES
('2011-10-31', 'Leaf Blowers', 45299);
INSERT INTO #f
VALUES
('2011-11-30', 'Leaf Blowers', 71815);
INSERT INTO #f
VALUES
('2011-12-31', 'Leaf Blowers', 45070);
INSERT INTO #f
VALUES
('2012-01-31', 'Leaf Blowers', 60712);
INSERT INTO #f
VALUES
('2012-02-29', 'Leaf Blowers', 50021);
INSERT INTO #f
VALUES
('2012-03-31', 'Leaf Blowers', 38495);
INSERT INTO #f
VALUES
('2012-04-30', 'Leaf Blowers', 49125);
INSERT INTO #f
VALUES
('2012-05-31', 'Leaf Blowers', 49227);
INSERT INTO #f
VALUES
('2012-06-30', 'Leaf Blowers', 61511);
INSERT INTO #f
VALUES
('2012-07-31', 'Leaf Blowers', 66185);
INSERT INTO #f
VALUES
('2012-08-31', 'Leaf Blowers', 59871);
INSERT INTO #f
VALUES
('2012-09-30', 'Leaf Blowers', 69951);
INSERT INTO #f
VALUES
('2012-10-31', 'Leaf Blowers', 84861);
INSERT INTO #f
VALUES
('2012-11-30', 'Leaf Blowers', 79946);
INSERT INTO #f
VALUES
('2010-10-31', 'Snow Blowers', 77554);
INSERT INTO #f
VALUES
('2010-11-30', 'Snow Blowers', 89677);
INSERT INTO #f
VALUES
('2010-12-31', 'Snow Blowers', 75063);
INSERT INTO #f
VALUES
('2011-01-31', 'Snow Blowers', 57609);
INSERT INTO #f
VALUES
('2011-02-28', 'Snow Blowers', 65206);
INSERT INTO #f
VALUES
('2011-03-31', 'Snow Blowers', 50178);
INSERT INTO #f
VALUES
('2011-04-30', 'Snow Blowers', 41676);
INSERT INTO #f
VALUES
('2011-05-31', 'Snow Blowers', 50024);
INSERT INTO #f
VALUES
('2011-06-30', 'Snow Blowers', 35835);
INSERT INTO #f
VALUES
('2011-07-31', 'Snow Blowers', 71655);
INSERT INTO #f
VALUES
('2011-08-31', 'Snow Blowers', 69309);
INSERT INTO #f
VALUES
('2011-09-30', 'Snow Blowers', 50066);
INSERT INTO #f
VALUES
('2011-10-31', 'Snow Blowers', 77390);
INSERT INTO #f
VALUES
('2011-11-30', 'Snow Blowers', 58315);
INSERT INTO #f
VALUES
('2011-12-31', 'Snow Blowers', 83867);
INSERT INTO #f
VALUES
('2012-01-31', 'Snow Blowers', 92994);
INSERT INTO #f
VALUES
('2012-02-29', 'Snow Blowers', 67718);
INSERT INTO #f
VALUES
('2012-03-31', 'Snow Blowers', 79875);
INSERT INTO #f
VALUES
('2012-04-30', 'Snow Blowers', 30774);
INSERT INTO #f
VALUES
('2012-05-31', 'Snow Blowers', 33199);
INSERT INTO #f
VALUES
('2012-06-30', 'Snow Blowers', 33284);
INSERT INTO #f
VALUES
('2012-07-31', 'Snow Blowers', 30369);
INSERT INTO #f
VALUES
('2012-08-31', 'Snow Blowers', 50885);
INSERT INTO #f
VALUES
('2012-09-30', 'Snow Blowers', 81832);
INSERT INTO #f
VALUES
('2012-10-31', 'Snow Blowers', 72875);
INSERT INTO #f
VALUES
('2012-11-30', 'Snow Blowers', 56955);
INSERT INTO #f
VALUES
('2010-10-31', 'Pool Supplies', 67437);
INSERT INTO #f
VALUES
('2010-11-30', 'Pool Supplies', 67760);
INSERT INTO #f
VALUES
('2010-12-31', 'Pool Supplies', 36603);
INSERT INTO #f
VALUES
('2011-01-31', 'Pool Supplies', 67072);
INSERT INTO #f
VALUES
('2011-02-28', 'Pool Supplies', 71843);
INSERT INTO #f
VALUES
('2011-03-31', 'Pool Supplies', 67283);
INSERT INTO #f
VALUES
('2011-04-30', 'Pool Supplies', 62408);
INSERT INTO #f
VALUES
('2011-05-31', 'Pool Supplies', 57671);
INSERT INTO #f
VALUES
('2011-06-30', 'Pool Supplies', 95730);
INSERT INTO #f
VALUES
('2011-07-31', 'Pool Supplies', 58017);
INSERT INTO #f
VALUES
('2011-08-31', 'Pool Supplies', 88317);
INSERT INTO #f
VALUES
('2011-09-30', 'Pool Supplies', 63141);
INSERT INTO #f
VALUES
('2011-10-31', 'Pool Supplies', 43968);
INSERT INTO #f
VALUES
('2011-11-30', 'Pool Supplies', 60566);
INSERT INTO #f
VALUES
('2011-12-31', 'Pool Supplies', 33517);
INSERT INTO #f
VALUES
('2012-01-31', 'Pool Supplies', 37272);
INSERT INTO #f
VALUES
('2012-02-29', 'Pool Supplies', 76982);
INSERT INTO #f
VALUES
('2012-03-31', 'Pool Supplies', 43459);
INSERT INTO #f
VALUES
('2012-04-30', 'Pool Supplies', 66698);
INSERT INTO #f
VALUES
('2012-05-31', 'Pool Supplies', 76722);
INSERT INTO #f
VALUES
('2012-06-30', 'Pool Supplies', 88796);
INSERT INTO #f
VALUES
('2012-07-31', 'Pool Supplies', 53017);
INSERT INTO #f
VALUES
('2012-08-31', 'Pool Supplies', 93040);
INSERT INTO #f
VALUES
('2012-09-30', 'Pool Supplies', 78513);
INSERT INTO #f
VALUES
('2012-10-31', 'Pool Supplies', 45990);
INSERT INTO #f
VALUES
('2012-11-30', 'Pool Supplies', 72321);
--Calculate the monthly FORECAST for Leaf Blowers
SELECT cast(EOM as date) as EOM,
       Item,
       SALES,
       CAST(wct.MovingFORECAST(
                                  RANK() OVER (ORDER BY EOM) + 1,
                                  sales,
                                  RANK() OVER (ORDER BY EOM),
                                  6,
                                  RANK() OVER (ORDER BY EOM),
                                  NULL
                              ) as money) as FORECAST
FROM #f
WHERE item = 'Leaf Blowers';
--Clean up
DROP TABLE #f;

This produces the following result.

EOMItemSALESFORECAST
2010-10-31Leaf Blowers42548.000.00
2010-11-30Leaf Blowers77227.00111906.00
2010-12-31Leaf Blowers66944.0086635.6667
2011-01-31Leaf Blowers34591.0046789.00
2011-02-28Leaf Blowers73468.0064716.80
2011-03-31Leaf Blowers50102.0056894.00
2011-04-30Leaf Blowers87270.0074084.2857
2011-05-31Leaf Blowers51555.0060043.4286
2011-06-30Leaf Blowers75139.0073054.8571
2011-07-31Leaf Blowers50682.0067982.1429
2011-08-31Leaf Blowers96577.0077592.7143
2011-09-30Leaf Blowers77553.0084138.8571
2011-10-31Leaf Blowers45299.0061656.5714
2011-11-30Leaf Blowers71815.0070941.5714
2011-12-31Leaf Blowers45070.0051845.1429
2012-01-31Leaf Blowers60712.0052720.8571
2012-02-29Leaf Blowers50021.0039066.8571
2012-03-31Leaf Blowers38495.0038590.2857
2012-04-30Leaf Blowers49125.0044332.2857
2012-05-31Leaf Blowers49227.0040370.5714
2012-06-30Leaf Blowers61511.0054231.1429
2012-07-31Leaf Blowers66185.0060772.4286
2012-08-31Leaf Blowers59871.0067393.00
2012-09-30Leaf Blowers69951.0075311.8571
2012-10-31Leaf Blowers84861.0083963.8571
2012-11-30Leaf Blowers79946.0087739.2857

In this example, we will set @x to NULL and @new_x to 7 to generate a forecast for the 7 month using the preceding 6 months.

SELECT cast(EOM as date) as EOM
,Item
,SALES
,CAST(wct.MovingFORECAST(7, sales, NULL,6,RANK() OVER (ORDER BY EOM),NULL) as money) as FORECAST
FROM #f
WHERE item = 'Leaf Blowers';

This produces the following result.

EOMItemSALESFORECAST
2010-10-31Leaf Blowers42548.00NULL
2010-11-30Leaf Blowers77227.00NULL
2010-12-31Leaf Blowers66944.00NULL
2011-01-31Leaf Blowers34591.00NULL
2011-02-28Leaf Blowers73468.00NULL
2011-03-31Leaf Blowers50102.00NULL
2011-04-30Leaf Blowers87270.0070997.1429
2011-05-31Leaf Blowers51555.0060788.1786
2011-06-30Leaf Blowers75139.0070472.1786
2011-07-31Leaf Blowers50682.0066086.8571
2011-08-31Leaf Blowers96577.0075508.5714
2011-09-30Leaf Blowers77553.0080564.0714
2011-10-31Leaf Blowers45299.0063530.8214
2011-11-30Leaf Blowers71815.0069942.6071
2011-12-31Leaf Blowers45070.0055388.6786
2012-01-31Leaf Blowers60712.0055530.2143
2012-02-29Leaf Blowers50021.0045266.1071
2012-03-31Leaf Blowers38495.0042834.3214
2012-04-30Leaf Blowers49125.0046125.5357
2012-05-31Leaf Blowers49227.0043294.5357
2012-06-30Leaf Blowers61511.0053321.9643
2012-07-31Leaf Blowers66185.0058982.0357
2012-08-31Leaf Blowers59871.0063917.4286
2012-09-30Leaf Blowers69951.0070568.3571
2012-10-31Leaf Blowers84861.0078713.2857
2012-11-30Leaf Blowers79946.0082645.6071

In this example we will look at the monthly sales and the sales forecast side-by-side for each of the three products.

SELECT cast(f1.EOM as date) as EOM
,f1.SALES as [LB]
,ROUND(wct.MovingFORECAST(7, f1.SALES, NULL, 6, ROW_NUMBER() OVER (ORDER BY f1.EOM),1), 0) as [FORECAST]
,f2.SALES as [SB]
,ROUND(wct.MovingFORECAST(7, f2.SALES, NULL, 6, ROW_NUMBER() OVER (ORDER BY f2.EOM),2), 0) as [FORECAST]
,f3.SALES as [PS]
,ROUND(wct.MovingFORECAST(7, f3.SALES, NULL, 6, ROW_NUMBER() OVER (ORDER BY f2.EOM),3), 0) as [FORECAST]
FROM #f f1
JOIN #f f2
ON f2.EOM = f1.EOM
AND f2.item = 'Snow Blowers'
JOIN #f f3
ON f3.EOM = f1.EOM
AND f3.Item = 'Pool Supplies'
WHERE f1.item = 'Leaf Blowers';

This produces the following result.

EOMLBFORECASTSBFORECASTPSFORECAST
2010-10-3142548.00NULL77554.00NULL67437.00NULL
2010-11-3077227.00NULL89677.00NULL67760.00NULL
2010-12-3166944.00NULL75063.00NULL36603.00NULL
2011-01-3134591.00NULL57609.00NULL67072.00NULL
2011-02-2873468.00NULL65206.00NULL71843.00NULL
2011-03-3150102.00NULL50178.00NULL67283.00NULL
2011-04-3087270.0064972.0041676.0064972.0062408.0064972.00
2011-05-3151555.0063829.0050024.0063829.0057671.0063829.00
2011-06-3075139.0081495.0035835.0081495.0095730.0081495.00
2011-07-3150682.0069753.0071655.0069753.0058017.0069753.00
2011-08-3196577.0078490.0069309.0078490.0088317.0078490.00
2011-09-3077553.0074624.0050066.0074624.0063141.0074624.00
2011-10-3145299.0061487.0077390.0061487.0043968.0061487.00
2011-11-3071815.0057161.0058315.0057161.0060566.0057161.00
2011-12-3145070.0039120.0083867.0039120.0033517.0039120.00
2012-01-3160712.0036284.0092994.0036284.0037272.0036284.00
2012-02-2950021.0047374.0067718.0047374.0076982.0047374.00
2012-03-3138495.0049524.0079875.0049524.0043459.0049524.00
2012-04-3049125.0060078.0030774.0060078.0066698.0060078.00
2012-05-3149227.0069426.0033199.0069426.0076722.0069426.00
2012-06-3061511.0085612.0033284.0085612.0088796.0085612.00
2012-07-3166185.0074434.0030369.0074434.0053017.0074434.00
2012-08-3159871.0080822.0050885.0080822.0093040.0080822.00
2012-09-3069951.0085836.0081832.0085836.0078513.0085836.00
2012-10-3184861.0066008.0072875.0066008.0045990.0066008.00
2012-11-3079946.0064773.0056955.0064773.0072321.0064773.00