Logo

MovingAVG

Updated 2023-11-13 20:47:43.520000

Syntax

SELECT [westclintech].[wct].[MovingAVG](
  <@Val, float,>
 ,<@Offset, int,>
 ,<@RowNum, int,>
 ,<@Id, tinyint,>)

Description

Use the scalar function 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.

Arguments

@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.

@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.

@Offset

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

@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.

Return Type

float

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, MovingAVG is 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.

Examples

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.

{"columns":[{"field":"num_check","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"date_check","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"payee"},{"field":"amt_check","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Moving AVG","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"num_check":"1200","date_check":"2012-01-31","payee":"Gas Company","amt_check":"108.49","Moving AVG":"108.49"},{"num_check":"1201","date_check":"2012-01-31","payee":"Electric Company","amt_check":"98.27","Moving AVG":"103.38"},{"num_check":"1202","date_check":"2012-01-31","payee":"Telephone Company","amt_check":"136.60","Moving AVG":"114.453333333333"},{"num_check":"1203","date_check":"2012-01-31","payee":"Car Leasing Company","amt_check":"361.75","Moving AVG":"176.2775"},{"num_check":"1204","date_check":"2012-01-31","payee":"Mortgage Bank","amt_check":"1129.23","Moving AVG":"366.868"},{"num_check":"1205","date_check":"2012-02-29","payee":"Gas Company","amt_check":"153.89","Moving AVG":"331.371666666667"},{"num_check":"1206","date_check":"2012-02-29","payee":"Electric Company","amt_check":"121.99","Moving AVG":"333.621666666667"},{"num_check":"1207","date_check":"2012-02-29","payee":"Telephone Company","amt_check":"138.92","Moving AVG":"340.396666666667"},{"num_check":"1208","date_check":"2012-02-29","payee":"Car Leasing Company","amt_check":"361.75","Moving AVG":"377.921666666667"},{"num_check":"1209","date_check":"2012-02-29","payee":"Mortgage Bank","amt_check":"1129.23","Moving AVG":"505.835"},{"num_check":"1210","date_check":"2012-03-31","payee":"Gas Company","amt_check":"70.79","Moving AVG":"329.428333333333"},{"num_check":"1211","date_check":"2012-03-31","payee":"Electric Company","amt_check":"93.57","Moving AVG":"319.375"},{"num_check":"1212","date_check":"2012-03-31","payee":"Telephone Company","amt_check":"149.78","Moving AVG":"324.006666666667"},{"num_check":"1213","date_check":"2012-03-31","payee":"Car Leasing Company","amt_check":"361.75","Moving AVG":"361.145"},{"num_check":"1214","date_check":"2012-03-31","payee":"Mortgage Bank","amt_check":"1129.23","Moving AVG":"489.058333333333"},{"num_check":"1215","date_check":"2012-04-30","payee":"Gas Company","amt_check":"105.58","Moving AVG":"318.45"},{"num_check":"1216","date_check":"2012-04-30","payee":"Electric Company","amt_check":"149.36","Moving AVG":"331.545"},{"num_check":"1217","date_check":"2012-04-30","payee":"Telephone Company","amt_check":"145.35","Moving AVG":"340.175"},{"num_check":"1218","date_check":"2012-04-30","payee":"Car Leasing Company","amt_check":"361.75","Moving AVG":"375.503333333333"},{"num_check":"1219","date_check":"2012-04-30","payee":"Mortgage Bank","amt_check":"1129.23","Moving AVG":"503.416666666667"},{"num_check":"1220","date_check":"2012-05-31","payee":"Gas Company","amt_check":"96.27","Moving AVG":"331.256666666667"},{"num_check":"1221","date_check":"2012-05-31","payee":"Electric Company","amt_check":"114.62","Moving AVG":"332.763333333333"},{"num_check":"1222","date_check":"2012-05-31","payee":"Telephone Company","amt_check":"145.43","Moving AVG":"332.108333333333"},{"num_check":"1223","date_check":"2012-05-31","payee":"Car Leasing Company","amt_check":"361.75","Moving AVG":"368.175"},{"num_check":"1224","date_check":"2012-05-31","payee":"Mortgage Bank","amt_check":"1129.23","Moving AVG":"496.088333333333"},{"num_check":"1225","date_check":"2012-06-30","payee":"Gas Company","amt_check":"147.03","Moving AVG":"332.388333333333"},{"num_check":"1226","date_check":"2012-06-30","payee":"Electric Company","amt_check":"130.52","Moving AVG":"338.096666666667"},{"num_check":"1227","date_check":"2012-06-30","payee":"Telephone Company","amt_check":"147.71","Moving AVG":"343.611666666667"},{"num_check":"1228","date_check":"2012-06-30","payee":"Car Leasing Company","amt_check":"361.75","Moving AVG":"379.665"},{"num_check":"1229","date_check":"2012-06-30","payee":"Mortgage Bank","amt_check":"1129.23","Moving AVG":"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.

{"columns":[{"field":"num_check","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"date_check","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"payee"},{"field":"amt_check","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Moving AVG","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"num_check":"1203","date_check":"31 Jan 2012","payee":"Car Leasing Company","amt_check":"361.75","Moving AVG":"361.75"},{"num_check":"1208","date_check":"29 Feb 2012","payee":"Car Leasing Company","amt_check":"361.75","Moving AVG":"361.75"},{"num_check":"1213","date_check":"31 Mar 2012","payee":"Car Leasing Company","amt_check":"361.75","Moving AVG":"361.75"},{"num_check":"1218","date_check":"30 Apr 2012","payee":"Car Leasing Company","amt_check":"361.75","Moving AVG":"361.75"},{"num_check":"1223","date_check":"31 May 2012","payee":"Car Leasing Company","amt_check":"361.75","Moving AVG":"361.75"},{"num_check":"1228","date_check":"30 Jun 2012","payee":"Car Leasing Company","amt_check":"361.75","Moving AVG":"361.75"},{"num_check":"1201","date_check":"31 Jan 2012","payee":"Electric Company","amt_check":"98.27","Moving AVG":"98.27"},{"num_check":"1206","date_check":"29 Feb 2012","payee":"Electric Company","amt_check":"121.99","Moving AVG":"110.13"},{"num_check":"1211","date_check":"31 Mar 2012","payee":"Electric Company","amt_check":"93.57","Moving AVG":"104.61"},{"num_check":"1216","date_check":"30 Apr 2012","payee":"Electric Company","amt_check":"149.36","Moving AVG":"115.8"},{"num_check":"1221","date_check":"31 May 2012","payee":"Electric Company","amt_check":"114.62","Moving AVG":"119.89"},{"num_check":"1226","date_check":"30 Jun 2012","payee":"Electric Company","amt_check":"130.52","Moving AVG":"122.02"},{"num_check":"1200","date_check":"31 Jan 2012","payee":"Gas Company","amt_check":"108.49","Moving AVG":"108.49"},{"num_check":"1205","date_check":"29 Feb 2012","payee":"Gas Company","amt_check":"153.89","Moving AVG":"131.19"},{"num_check":"1210","date_check":"31 Mar 2012","payee":"Gas Company","amt_check":"70.79","Moving AVG":"111.06"},{"num_check":"1215","date_check":"30 Apr 2012","payee":"Gas Company","amt_check":"105.58","Moving AVG":"109.69"},{"num_check":"1220","date_check":"31 May 2012","payee":"Gas Company","amt_check":"96.27","Moving AVG":"106.63"},{"num_check":"1225","date_check":"30 Jun 2012","payee":"Gas Company","amt_check":"147.03","Moving AVG":"104.92"},{"num_check":"1204","date_check":"31 Jan 2012","payee":"Mortgage Bank","amt_check":"1129.23","Moving AVG":"1129.23"},{"num_check":"1209","date_check":"29 Feb 2012","payee":"Mortgage Bank","amt_check":"1129.23","Moving AVG":"1129.23"},{"num_check":"1214","date_check":"31 Mar 2012","payee":"Mortgage Bank","amt_check":"1129.23","Moving AVG":"1129.23"},{"num_check":"1219","date_check":"30 Apr 2012","payee":"Mortgage Bank","amt_check":"1129.23","Moving AVG":"1129.23"},{"num_check":"1224","date_check":"31 May 2012","payee":"Mortgage Bank","amt_check":"1129.23","Moving AVG":"1129.23"},{"num_check":"1229","date_check":"30 Jun 2012","payee":"Mortgage Bank","amt_check":"1129.23","Moving AVG":"1129.23"},{"num_check":"1202","date_check":"31 Jan 2012","payee":"Telephone Company","amt_check":"136.60","Moving AVG":"136.6"},{"num_check":"1207","date_check":"29 Feb 2012","payee":"Telephone Company","amt_check":"138.92","Moving AVG":"137.76"},{"num_check":"1212","date_check":"31 Mar 2012","payee":"Telephone Company","amt_check":"149.78","Moving AVG":"141.77"},{"num_check":"1217","date_check":"30 Apr 2012","payee":"Telephone Company","amt_check":"145.35","Moving AVG":"142.66"},{"num_check":"1222","date_check":"31 May 2012","payee":"Telephone Company","amt_check":"145.43","Moving AVG":"144.87"},{"num_check":"1227","date_check":"30 Jun 2012","payee":"Telephone Company","amt_check":"147.71","Moving AVG":"147.07"}]}