MovingFORECAST
Updated 2023-11-13 21:15:15.150000
Syntax
SELECT [westclintech].[wct].[MovingFORECAST](
<@New_x, float,>
,<@Y, float,>
,<@X, float,>
,<@Offset, int,>
,<@RowNum, int,>
,<@Id, tinyint,>)
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.
Arguments
@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.
@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.
@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.
@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.
@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.
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.
{"columns":[{"field":"EOM","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Item"},{"field":"SALES","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"FORECAST","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"EOM":"2010-10-31","Item":"Leaf Blowers","SALES":"42548.00","FORECAST":"0.00"},{"EOM":"2010-11-30","Item":"Leaf Blowers","SALES":"77227.00","FORECAST":"111906.00"},{"EOM":"2010-12-31","Item":"Leaf Blowers","SALES":"66944.00","FORECAST":"86635.6667"},{"EOM":"2011-01-31","Item":"Leaf Blowers","SALES":"34591.00","FORECAST":"46789.00"},{"EOM":"2011-02-28","Item":"Leaf Blowers","SALES":"73468.00","FORECAST":"64716.80"},{"EOM":"2011-03-31","Item":"Leaf Blowers","SALES":"50102.00","FORECAST":"56894.00"},{"EOM":"2011-04-30","Item":"Leaf Blowers","SALES":"87270.00","FORECAST":"74084.2857"},{"EOM":"2011-05-31","Item":"Leaf Blowers","SALES":"51555.00","FORECAST":"60043.4286"},{"EOM":"2011-06-30","Item":"Leaf Blowers","SALES":"75139.00","FORECAST":"73054.8571"},{"EOM":"2011-07-31","Item":"Leaf Blowers","SALES":"50682.00","FORECAST":"67982.1429"},{"EOM":"2011-08-31","Item":"Leaf Blowers","SALES":"96577.00","FORECAST":"77592.7143"},{"EOM":"2011-09-30","Item":"Leaf Blowers","SALES":"77553.00","FORECAST":"84138.8571"},{"EOM":"2011-10-31","Item":"Leaf Blowers","SALES":"45299.00","FORECAST":"61656.5714"},{"EOM":"2011-11-30","Item":"Leaf Blowers","SALES":"71815.00","FORECAST":"70941.5714"},{"EOM":"2011-12-31","Item":"Leaf Blowers","SALES":"45070.00","FORECAST":"51845.1429"},{"EOM":"2012-01-31","Item":"Leaf Blowers","SALES":"60712.00","FORECAST":"52720.8571"},{"EOM":"2012-02-29","Item":"Leaf Blowers","SALES":"50021.00","FORECAST":"39066.8571"},{"EOM":"2012-03-31","Item":"Leaf Blowers","SALES":"38495.00","FORECAST":"38590.2857"},{"EOM":"2012-04-30","Item":"Leaf Blowers","SALES":"49125.00","FORECAST":"44332.2857"},{"EOM":"2012-05-31","Item":"Leaf Blowers","SALES":"49227.00","FORECAST":"40370.5714"},{"EOM":"2012-06-30","Item":"Leaf Blowers","SALES":"61511.00","FORECAST":"54231.1429"},{"EOM":"2012-07-31","Item":"Leaf Blowers","SALES":"66185.00","FORECAST":"60772.4286"},{"EOM":"2012-08-31","Item":"Leaf Blowers","SALES":"59871.00","FORECAST":"67393.00"},{"EOM":"2012-09-30","Item":"Leaf Blowers","SALES":"69951.00","FORECAST":"75311.8571"},{"EOM":"2012-10-31","Item":"Leaf Blowers","SALES":"84861.00","FORECAST":"83963.8571"},{"EOM":"2012-11-30","Item":"Leaf Blowers","SALES":"79946.00","FORECAST":"87739.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.
{"columns":[{"field":"EOM","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Item"},{"field":"SALES","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"FORECAST"}],"rows":[{"EOM":"2010-10-31","Item":"Leaf Blowers","SALES":"42548.00","FORECAST":"NULL"},{"EOM":"2010-11-30","Item":"Leaf Blowers","SALES":"77227.00","FORECAST":"NULL"},{"EOM":"2010-12-31","Item":"Leaf Blowers","SALES":"66944.00","FORECAST":"NULL"},{"EOM":"2011-01-31","Item":"Leaf Blowers","SALES":"34591.00","FORECAST":"NULL"},{"EOM":"2011-02-28","Item":"Leaf Blowers","SALES":"73468.00","FORECAST":"NULL"},{"EOM":"2011-03-31","Item":"Leaf Blowers","SALES":"50102.00","FORECAST":"NULL"},{"EOM":"2011-04-30","Item":"Leaf Blowers","SALES":"87270.00","FORECAST":"70997.1429"},{"EOM":"2011-05-31","Item":"Leaf Blowers","SALES":"51555.00","FORECAST":"60788.1786"},{"EOM":"2011-06-30","Item":"Leaf Blowers","SALES":"75139.00","FORECAST":"70472.1786"},{"EOM":"2011-07-31","Item":"Leaf Blowers","SALES":"50682.00","FORECAST":"66086.8571"},{"EOM":"2011-08-31","Item":"Leaf Blowers","SALES":"96577.00","FORECAST":"75508.5714"},{"EOM":"2011-09-30","Item":"Leaf Blowers","SALES":"77553.00","FORECAST":"80564.0714"},{"EOM":"2011-10-31","Item":"Leaf Blowers","SALES":"45299.00","FORECAST":"63530.8214"},{"EOM":"2011-11-30","Item":"Leaf Blowers","SALES":"71815.00","FORECAST":"69942.6071"},{"EOM":"2011-12-31","Item":"Leaf Blowers","SALES":"45070.00","FORECAST":"55388.6786"},{"EOM":"2012-01-31","Item":"Leaf Blowers","SALES":"60712.00","FORECAST":"55530.2143"},{"EOM":"2012-02-29","Item":"Leaf Blowers","SALES":"50021.00","FORECAST":"45266.1071"},{"EOM":"2012-03-31","Item":"Leaf Blowers","SALES":"38495.00","FORECAST":"42834.3214"},{"EOM":"2012-04-30","Item":"Leaf Blowers","SALES":"49125.00","FORECAST":"46125.5357"},{"EOM":"2012-05-31","Item":"Leaf Blowers","SALES":"49227.00","FORECAST":"43294.5357"},{"EOM":"2012-06-30","Item":"Leaf Blowers","SALES":"61511.00","FORECAST":"53321.9643"},{"EOM":"2012-07-31","Item":"Leaf Blowers","SALES":"66185.00","FORECAST":"58982.0357"},{"EOM":"2012-08-31","Item":"Leaf Blowers","SALES":"59871.00","FORECAST":"63917.4286"},{"EOM":"2012-09-30","Item":"Leaf Blowers","SALES":"69951.00","FORECAST":"70568.3571"},{"EOM":"2012-10-31","Item":"Leaf Blowers","SALES":"84861.00","FORECAST":"78713.2857"},{"EOM":"2012-11-30","Item":"Leaf Blowers","SALES":"79946.00","FORECAST":"82645.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.
{"columns":[{"field":"EOM","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"LB","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"FORECAST"},{"field":"SB","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"FORECAST"},{"field":"PS","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"FORECAST"}],"rows":[{"EOM":"2010-10-31","LB":"42548.00","FORECAST":"NULL","SB":"77554.00","PS":"67437.00"},{"EOM":"2010-11-30","LB":"77227.00","FORECAST":"NULL","SB":"89677.00","PS":"67760.00"},{"EOM":"2010-12-31","LB":"66944.00","FORECAST":"NULL","SB":"75063.00","PS":"36603.00"},{"EOM":"2011-01-31","LB":"34591.00","FORECAST":"NULL","SB":"57609.00","PS":"67072.00"},{"EOM":"2011-02-28","LB":"73468.00","FORECAST":"NULL","SB":"65206.00","PS":"71843.00"},{"EOM":"2011-03-31","LB":"50102.00","FORECAST":"NULL","SB":"50178.00","PS":"67283.00"},{"EOM":"2011-04-30","LB":"87270.00","FORECAST":"64972.00","SB":"41676.00","PS":"62408.00"},{"EOM":"2011-05-31","LB":"51555.00","FORECAST":"63829.00","SB":"50024.00","PS":"57671.00"},{"EOM":"2011-06-30","LB":"75139.00","FORECAST":"81495.00","SB":"35835.00","PS":"95730.00"},{"EOM":"2011-07-31","LB":"50682.00","FORECAST":"69753.00","SB":"71655.00","PS":"58017.00"},{"EOM":"2011-08-31","LB":"96577.00","FORECAST":"78490.00","SB":"69309.00","PS":"88317.00"},{"EOM":"2011-09-30","LB":"77553.00","FORECAST":"74624.00","SB":"50066.00","PS":"63141.00"},{"EOM":"2011-10-31","LB":"45299.00","FORECAST":"61487.00","SB":"77390.00","PS":"43968.00"},{"EOM":"2011-11-30","LB":"71815.00","FORECAST":"57161.00","SB":"58315.00","PS":"60566.00"},{"EOM":"2011-12-31","LB":"45070.00","FORECAST":"39120.00","SB":"83867.00","PS":"33517.00"},{"EOM":"2012-01-31","LB":"60712.00","FORECAST":"36284.00","SB":"92994.00","PS":"37272.00"},{"EOM":"2012-02-29","LB":"50021.00","FORECAST":"47374.00","SB":"67718.00","PS":"76982.00"},{"EOM":"2012-03-31","LB":"38495.00","FORECAST":"49524.00","SB":"79875.00","PS":"43459.00"},{"EOM":"2012-04-30","LB":"49125.00","FORECAST":"60078.00","SB":"30774.00","PS":"66698.00"},{"EOM":"2012-05-31","LB":"49227.00","FORECAST":"69426.00","SB":"33199.00","PS":"76722.00"},{"EOM":"2012-06-30","LB":"61511.00","FORECAST":"85612.00","SB":"33284.00","PS":"88796.00"},{"EOM":"2012-07-31","LB":"66185.00","FORECAST":"74434.00","SB":"30369.00","PS":"53017.00"},{"EOM":"2012-08-31","LB":"59871.00","FORECAST":"80822.00","SB":"50885.00","PS":"93040.00"},{"EOM":"2012-09-30","LB":"69951.00","FORECAST":"85836.00","SB":"81832.00","PS":"78513.00"},{"EOM":"2012-10-31","LB":"84861.00","FORECAST":"66008.00","SB":"72875.00","PS":"45990.00"},{"EOM":"2012-11-30","LB":"79946.00","FORECAST":"64773.00","SB":"56955.00","PS":"72321.00"}]}