MAX
Description
MAX
is a window aggregate function that partitions rows into groups, orders rows by a field, and returns the maximum (highest) value in the group.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 (maximum for this function) in each group.
Syntax
whereMAX(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
The return value is the same field type as the input value.
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 aMAXexpression.
- 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 (maximum 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
Example: You have a dataset with these rows and fields.
Supervisory Org | Quarter | Name | Comp Change |
|---|---|---|---|
Marketing | 2019-Q1 | Goh | 2000.00 |
Marketing | 2019-Q1 | Freeman | 1000.00 |
Marketing | 2019-Q1 | Smith | 2500.00 |
Consulting | 2019-Q1 | Gomez | 5000.00 |
Consulting | 2019-Q1 | Kimura | 3000.00 |
Consulting | 2019-Q1 | Fitz | 3500.00 |
Consulting | 2019-Q2 | Gomez | 0 |
Consulting | 2019-Q2 | Kimura | 2000.00 |
Consulting | 2019-Q2 | Fitz | 1500.00 |
You can calculate the highest change in compensation (
Comp Change
field) for each supervisory org in each quarter.To ensure that Workday returns the same value for every row in a partition, order the rows in descending (
DESC
) order by the same field as the input field so the highest compensation change comes first in each partition.Use this expression in the
Max Comp Change
field:MAX([Comp Change]) OVER( PARTITION BY [Supervisory Org], [Quarter] ORDER BY [Comp Change] DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING )
You get these results:
Supervisory Org | Quarter | Name | Comp Change | Max Comp Change |
|---|---|---|---|---|
Consulting | 2019-Q1 | Gomez | 5000.00 | 5000.00 |
Consulting | 2019-Q1 | Fitz | 3500.00 | 5000.00 |
Consulting | 2019-Q1 | Kimura | 3000.00 | 5000.00 |
Consulting | 2019-Q2 | Kimura | 2000.00 | 2000.00 |
Consulting | 2019-Q2 | Fitz | 1500.00 | 2000.00 |
Consulting | 2019-Q2 | Gomez | 0 | 2000.00 |
Marketing | 2019-Q1 | Smith | 2500.00 | 2500.00 |
Marketing | 2019-Q1 | Goh | 2000.00 | 2500.00 |
Marketing | 2019-Q1 | Freeman | 1000.00 | 2500.00 |