MovingAVG
Updated: 31 Oct 2012
Use MovingAVG to calculate the moving average of column values in an ordered resultant table, without having to do a self-join. The moving average value is calculated for each value from the first value in the window to the last value in the window. The size of the window specified in the function. If the column values are presented to the functions out of order, an error message will be generated.
Syntax
SELECT [Example].[wct].[MovingAVG](
<@Val, float,>
,<@Offset, int,>
,<@RowNum, int,>
,<@Id, tinyint,>)
GO
Arguments
@Val
the value passed into the function. @Val 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 average 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 MovingAVG calculation. @Id allows you to specify multiple moving averages within a resultant table. @Id is an expression of type tinyint or of a type that can be implicitly converted to tinyint.
Remarks
· If @Id is NULL then @Id = 0.
· To calculate running averages from the beginning of the dataset or parition, use the RunningAVG function.
· To calculate moving averages for time series, use the DEMA or DWMA function.
· If @RowNum is equal to 1, MovingAVGis equal to @Val.
· @RowNum must be in ascending order.
· 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.
Example
In this example we will calculate the average check amount for the current and preceding 5 rows in a check register. We will create a temporary table, #c, populate it with some data and then run the SELECT.
/*Create the temporary table*/
CREATE TABLE #c (
num_check int,
date_check datetime,
payee varchar(50),
amt_check money,
Primary Key (num_check)
)
/*Populate the table with some data*/
INSERT INTO #c VALUES (1200,'2012-01-31','Gas Company',108.49)
INSERT INTO #c VALUES (1201,'2012-01-31','Electric Company',98.27)
INSERT INTO #c VALUES (1202,'2012-01-31','Telephone Company',136.6)
INSERT INTO #c VALUES (1203,'2012-01-31','Car Leasing Company',361.75)
INSERT INTO #c VALUES (1204,'2012-01-31','Mortgage Bank',1129.23)
INSERT INTO #c VALUES (1205,'2012-02-29','Gas Company',153.89)
INSERT INTO #c VALUES (1206,'2012-02-29','Electric Company',121.99)
INSERT INTO #c VALUES (1207,'2012-02-29','Telephone Company',138.92)
INSERT INTO #c VALUES (1208,'2012-02-29','Car Leasing Company',361.75)
INSERT INTO #c VALUES (1209,'2012-02-29','Mortgage Bank',1129.23)
INSERT INTO #c VALUES (1210,'2012-03-31','Gas Company',70.79)
INSERT INTO #c VALUES (1211,'2012-03-31','Electric Company',93.57)
INSERT INTO #c VALUES (1212,'2012-03-31','Telephone Company',149.78)
INSERT INTO #c VALUES (1213,'2012-03-31','Car Leasing Company',361.75)
INSERT INTO #c VALUES (1214,'2012-03-31','Mortgage Bank',1129.23)
INSERT INTO #c VALUES (1215,'2012-04-30','Gas Company',105.58)
INSERT INTO #c VALUES (1216,'2012-04-30','Electric Company',149.36)
INSERT INTO #c VALUES (1217,'2012-04-30','Telephone Company',145.35)
INSERT INTO #c VALUES (1218,'2012-04-30','Car Leasing Company',361.75)
INSERT INTO #c VALUES (1219,'2012-04-30','Mortgage Bank',1129.23)
INSERT INTO #c VALUES (1220,'2012-05-31','Gas Company',96.27)
INSERT INTO #c VALUES (1221,'2012-05-31','Electric Company',114.62)
INSERT INTO #c VALUES (1222,'2012-05-31','Telephone Company',145.43)
INSERT INTO #c VALUES (1223,'2012-05-31','Car Leasing Company',361.75)
INSERT INTO #c VALUES (1224,'2012-05-31','Mortgage Bank',1129.23)
INSERT INTO #c VALUES (1225,'2012-06-30','Gas Company',147.03)
INSERT INTO #c VALUES (1226,'2012-06-30','Electric Company',130.52)
INSERT INTO #c VALUES (1227,'2012-06-30','Telephone Company',147.71)
INSERT INTO #c VALUES (1228,'2012-06-30','Car Leasing Company',361.75)
INSERT INTO #c VALUES (1229,'2012-06-30','Mortgage Bank',1129.23)
/*Calculate the moving average*/
SELECT num_check
,cast(date_check as date)as date_check
,cast(payee as char(20)) as payee
,amt_check
,wct.MovingAVG(amt_check,5,num_check,NULL) as [Moving AVG]
FROM #c
--Clean up
DROP TABLE #c
This produces the following result.
num_check date_check payee amt_check Moving AVG
----------- ---------- -------------------- --------------------- ----------------------
1200 2012-01-31 Gas Company 108.49 108.49
1201 2012-01-31 Electric Company 98.27 103.38
1202 2012-01-31 Telephone Company 136.60 114.453333333333
1203 2012-01-31 Car Leasing Company 361.75 176.2775
1204 2012-01-31 Mortgage Bank 1129.23 366.868
1205 2012-02-29 Gas Company 153.89 331.371666666667
1206 2012-02-29 Electric Company 121.99 333.621666666667
1207 2012-02-29 Telephone Company 138.92 340.396666666667
1208 2012-02-29 Car Leasing Company 361.75 377.921666666667
1209 2012-02-29 Mortgage Bank 1129.23 505.835
1210 2012-03-31 Gas Company 70.79 329.428333333333
1211 2012-03-31 Electric Company 93.57 319.375
1212 2012-03-31 Telephone Company 149.78 324.006666666667
1213 2012-03-31 Car Leasing Company 361.75 361.145
1214 2012-03-31 Mortgage Bank 1129.23 489.058333333333
1215 2012-04-30 Gas Company 105.58 318.45
1216 2012-04-30 Electric Company 149.36 331.545
1217 2012-04-30 Telephone Company 145.35 340.175
1218 2012-04-30 Car Leasing Company 361.75 375.503333333333
1219 2012-04-30 Mortgage Bank 1129.23 503.416666666667
1220 2012-05-31 Gas Company 96.27 331.256666666667
1221 2012-05-31 Electric Company 114.62 332.763333333333
1222 2012-05-31 Telephone Company 145.43 332.108333333333
1223 2012-05-31 Car Leasing Company 361.75 368.175
1224 2012-05-31 Mortgage Bank 1129.23 496.088333333333
1225 2012-06-30 Gas Company 147.03 332.388333333333
1226 2012-06-30 Electric Company 130.52 338.096666666667
1227 2012-06-30 Telephone Company 147.71 343.611666666667
1228 2012-06-30 Car Leasing Company 361.75 379.665
1229 2012-06-30 Mortgage Bank 1129.23 507.578333333333
In this example, we calculate the average by payee in date order and use the ROW_NUMBER() function to determine the @RowNum value passed into the MovingAVG function.
SELECT num_check
,cast(convert(varchar, date_check, 106) as char(11)) as date_check
,cast(payee as char(20)) as payee
,amt_check
,ROUND(wct.MovingAvg(amt_check, 3, ROW_NUMBER() OVER (PARTITION BY payee ORDER BY payee, num_check),NULL), 2) as [Moving AVG]
FROM #c
ORDER BY payee, num_check
This produces the following result.
num_check date_check payee amt_check Moving AVG
----------- ----------- -------------------- --------------------- ----------------------
1203 31 Jan 2012 Car Leasing Company 361.75 361.75
1208 29 Feb 2012 Car Leasing Company 361.75 361.75
1213 31 Mar 2012 Car Leasing Company 361.75 361.75
1218 30 Apr 2012 Car Leasing Company 361.75 361.75
1223 31 May 2012 Car Leasing Company 361.75 361.75
1228 30 Jun 2012 Car Leasing Company 361.75 361.75
1201 31 Jan 2012 Electric Company 98.27 98.27
1206 29 Feb 2012 Electric Company 121.99 110.13
1211 31 Mar 2012 Electric Company 93.57 104.61
1216 30 Apr 2012 Electric Company 149.36 115.8
1221 31 May 2012 Electric Company 114.62 119.89
1226 30 Jun 2012 Electric Company 130.52 122.02
1200 31 Jan 2012 Gas Company 108.49 108.49
1205 29 Feb 2012 Gas Company 153.89 131.19
1210 31 Mar 2012 Gas Company 70.79 111.06
1215 30 Apr 2012 Gas Company 105.58 109.69
1220 31 May 2012 Gas Company 96.27 106.63
1225 30 Jun 2012 Gas Company 147.03 104.92
1204 31 Jan 2012 Mortgage Bank 1129.23 1129.23
1209 29 Feb 2012 Mortgage Bank 1129.23 1129.23
1214 31 Mar 2012 Mortgage Bank 1129.23 1129.23
1219 30 Apr 2012 Mortgage Bank 1129.23 1129.23
1224 31 May 2012 Mortgage Bank 1129.23 1129.23
1229 30 Jun 2012 Mortgage Bank 1129.23 1129.23
1202 31 Jan 2012 Telephone Company 136.60 136.6
1207 29 Feb 2012 Telephone Company 138.92 137.76
1212 31 Mar 2012 Telephone Company 149.78 141.77
1217 30 Apr 2012 Telephone Company 145.35 142.66
1222 31 May 2012 Telephone Company 145.43 144.87
1227 30 Jun 2012 Telephone Company 147.71 147.07