# ROLLINGSUM Function

Computes the rolling sum of values forward or backward of the current row within the specified column.
• If an input value is missing or null, it is not factored in the computation. For example, for the first row in the dataset, the rolling sum of previous values is the value in the first row.
• The row from which to extract a value is determined by the order in which the rows are organized based on the `order` parameter.

• If you are working on a randomly generated sample of your dataset, the values that you see for this function might not correspond to the values that are generated on the full dataset during job execution.

• The function takes a column name and two optional integer parameters that determine the window backward and forward of the current row.
• The default integer parameter values are `-1` and `0`, which computes the rolling average from the current row back to the first row of the dataset.
• This function works with the following transforms:

## Basic Usage

Column example:

`derive type:single value:ROLLINGSUM(myCol)`

Output: Generates a new column containing the rolling sum of all values in the `myCol` column from the first row of the dataset to the current one.

Rows before example:

`window value:ROLLINGSUM(myNumber, 3)`

Output: Generates the new column, which contains the rolling sum of the current row and the three previous row values in the `myNumber` column.

Rows before and after example:

`window value:ROLLINGSUM(myNumber, 3, 2)`

Output: Generates the new column , which contains the rolling sum of the three previous row values, the current row value, and the two rows after the current one in the `myNumber` column.

## Syntax

`window value:ROLLINGSUM(col_ref, rowsBefore_integer, rowsAfter_integer) order: order_col [group: group_col]`

ArgumentRequired?Data TypeDescription
col_refYstringName of column whose values are applied to the function
rowsBefore_integerNintegerNumber of rows before the current one to include in the computation.
rowsAfter_integerNintegerNumber of rows after the current one to include in the computation

For more information on the `order` and `group` parameters, see Window Transform.

### col_ref

Name of the column whose values are used to compute the rolling sum.

• Multiple columns and wildcards are not supported.

Usage Notes:

Required?Data TypeExample Value
YesString (column reference to Integer or Decimal values)`myColumn`

### rowsBefore_integer, rowsAfter_integer

Integers representing the number of rows before or after the current one from which to compute the rolling sum, including the current row. For example, if the first value is `5`, the current row and the five rows before it are used in the computation. Negative values for `k` compute the rolling average from rows preceding the current one.

• `rowBefore=0` generates the current row value only.
• `rowBefore=-1` uses all rows preceding the current one.
• If `rowsAfter` is not specified, then the value `0` is applied.
• If a `group` parameter is applied, then these parameter values should be no more than the maximum number of rows in the groups.

Usage Notes:

Required?Data TypeExample Value
NoInteger`4`

## Examples

### Example - Rolling window functions

This example describes how to use the rolling computational functions:

• `ROLLINGSUM` - computes a rolling sum from a window of rows before and after the current row. See ROLLINGSUM Function.
• `ROLLINGAVERAGE` - computes a rolling average from a window of rows before and after the current row. See ROLLINGAVERAGE Function.
• `ROWNUMBER` - computes the row number for each row, as determined by the ordering column. See ROWNUMBER Function.

The following dataset contains sales data over the final quarter of the year.

Source:

DateSales
10/2/16200
10/9/16500
10/16/16350
10/23/16400
10/30/16190
11/6/16550
11/13/16610
11/20/16480
11/27/16660
12/4/16690
12/11/16810
12/18/16950
12/25/161020
1/1/17680

Transform:

First, you want to maintain the row information as a separate column. Since data is ordered already by the `Date` column, you can use the following:

`window value:ROWNUMBER() order:Date`

Rename this column to `rowId` for week of quarter.

Now, you want to extract month and week information from the `Date` values. Deriving the month value:

`derive type:single value:MONTH(Date) as:'Month'`

Deriving the quarter value:

`derive type:single value:(1 + FLOOR(((month-1)/3))) as:'QTR'`

Deriving the week-of-quarter value:

`window value:ROWNUMBER() order:Date group:QTR`

Rename this column `WOQ` (week of quarter).

Deriving the week-of-month value:

`window value:ROWNUMBER() group:Month order:Date`

Rename this column `WOM` (week of month).

Now, you perform your rolling computations. Compute the running total of sales using the following:

`window value: ROLLINGSUM(Sales, -1, 0) order: Date group:QTR`

The `-1` parameter is used in the above computation to gather the rolling sum of all rows of data from the current one to the first one. Note that the use of the `QTR` column for grouping, which moves the value for the `01/01/2017` into its own computational bucket. This may or may not be preferred.

Rename this column `QTD` (quarter to-date). Now, generate a similar column to compute the rolling average of weekly sales for the quarter:

`window value: ROUND(ROLLINGAVERAGE(Sales, -1, 0)) order: Date group:QTR`

Since the `ROLLINGAVERAGE` function can compute fractional values, it is wrapped in the `ROUND` function for neatness. Rename this column `avgWeekByQuarter`.

Results:

When the unnecessary columns are dropped and some reordering is applied, your dataset should look like the following:

DateWOQSalesQTDavgWeekByQuarter
10/2/161200200200
10/9/162500700350
10/16/1633501050350
10/23/1644001450363
10/30/1651901640328
11/6/1665502190365
11/13/1676102800400
11/20/1684803280410
11/27/1696603940438
12/4/16106904630463
12/11/16118105440495
12/18/16129506390533
12/25/161310207410570
1/1/171680680680

Var denne siden nyttig? Si fra hva du synes: