AVG
Description
AVG
is a window aggregate function that partitions rows into groups, orders rows by a field, and returns the average of all valid numeric values in the group. It sums all values in the group and divides by the number of valid (NOT NULL) rows. You can use AVG
to calculate moving averages.The
PARTITION BY
clause determines which fields to use to partition a set of input rows into groups. The ORDER BY
clause determines how to order the rows in the partition.Workday separates the input rows into groups according to the partitioning fields, orders the rows according to the ordering fields, and then computes the aggregate expression (average for this function) in each group.
Syntax
whereAVG(input_field)OVER(PARTITION BYpartitioning_field[,partitioning_field]ORDER BYordering_field[ASC | DESC] [,ordering_field[ASC | DESC]]RANGEBETWEENvaluePRECEDINGANDCURRENT ROW|ROWSwin_boundary|BETWEENwin_boundaryANDwin_boundary)win_boundarycan be:UNBOUNDED PRECEDINGvaluePRECEDINGUNBOUNDED FOLLOWINGvalueFOLLOWINGCURRENT ROW
Return Value
Returns a value of type
NUMERIC
or DOUBLE
depending on the type of input_field
.Input Parameters
- input_field
- Required. The field on which to perform the aggregate function. You can use any numeric field or a Currency field.
- OVER()
- Required.OVERmust be used within anAVGexpression.
- PARTITION BYpartitioning_field
- Required. Use thePARTITION BYclause to specify 1 or more fields to use to partition a group of input rows. You can specify any field type except Currency. Example: You specify the Month field as the partitioning field, so Workday groups into a single partition all records that have the same value for Month.
- ORDER BYordering_field
- Required. Use theORDER BYclause to specify how to order the input rows in the partition using the values in the specified field within each partition. You can specify any field type except Currency. However, you must use a numeric field type, such as Integer or Numeric when you use theRANGEclause.You can use theDESCorASCkeywords to sort in descending order (high to low values, NULLs are last) or ascending order (low to high values, NULLs are first) for each ordering field. If you don't specify a sort order for an ordering field, Workday automatically sorts rows in ascending order.
- ROWS | RANGE
- Required. TheROWSandRANGEclauses define the specific number of rows (relative to the current row) within the partition by specifying a window frame. You define the window frame by specifying start and end points within the partition, known as window boundaries. The window frame is the set of input rows in each partition over which to calculate the aggregate expression (average for this function). The window frame can include one, several, or all rows of the partition.BothROWSandRANGEspecify the range of rows relative to the current row, butRANGEoperates logically on values (logical association) andROWSoperates physically on rows in the dataset (physical association).RANGElimits the window frame to contain rows that have their values within the specified range, relative to the current value.ROWSlimits the window frame to contain rows that are physically next to the current row.UseRANGEto define absolute window boundaries, such as the past 3 months or year to date. When you useRANGE, theORDER BYclause must use a numeric field type, such as Integer or Numeric.Example: Suppose you have an Integer field called MonthNum that represents the number of the month in the year (values 1 to 12). To specify all values from the past 3 months, you would order by MonthNum and useRANGE BETWEEN 2 PRECEDING AND CURRENT ROW. This RANGE clause includes the current month and the previous 2 months, resulting in 3 months total.When you publish a dataset that contains a window function usingRANGE, the number of rows in the window must be 1000 or less. If a particular window exceeds 1000 rows, the publish job fails.
- win_boundary
- Required. The window boundaries define the start and end points of the window frame. Window boundaries are relative to the current row.APRECEDINGclause defines a window boundary that is lower than the current row (the number of rows to include before the current row). TheFOLLOWINGclause defines a window boundary that is greater than the current row (the number of rows to include after the current row).If you specify only 1 window boundary, then Workday uses the current row as the other boundary in the window frame (either the upper or lower boundary depending on the expression syntax). TheUNBOUNDEDkeyword includes all rows in the direction specified. When you need to specify both a start and end of a window frame, use theBETWEENandANDkeywords.When specifying a specific number of rows, thevaluemust be 100 or less.Example:ROWS 2 PRECEDINGmeans that the window is 3 rows in size, starting with 2 rows preceding until and including the current row.Example:ROWS UNBOUNDED FOLLOWINGmeans that the window starts with the current row and includes the current row and all rows that come after the current row.
Examples
You can calculate the moving average (rolling average or running average) sales for each employee:
AVG([Sales]) OVER( PARTITION BY [Employee] ORDER BY [SalesDate] DESC ROWS UNBOUNDED PRECEDING)
You can calculate the overall average sales for every row in the partition, regardless of the fields in the ORDER BY clause:
AVG([Sales]) OVER( PARTITION BY [Employee] ORDER BY [SalesDate] DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
You can calculate the rolling 12 month average:
AVG([fieldA]) OVER( PARTITION BY [fieldB] ORDER BY [Month] RANGE 11 PRECEDING)
The
Month
field must be a numeric field type, such as Integer or Numeric.You can calculate the previous year to date average:
AVG([fieldA]) OVER( PARTITION BY [fieldB] ORDER BY [Year] RANGE 1 PRECEDING)
The
Year
field must be a numeric field type, such as Integer or Numeric.