Thinking back to the standard aggregations in the previous courses, the first 8 functions should be quite intuitive to understand. The syntax starts always the same:
PU_X(target_table, source_table.column) with X being the aggregation you want to perform
In addition, there are three PU-functions that don’t correspond to a regular aggregation: PU_FIRST, PU_LAST and PU_STRING_AGG.
Click through the tabs to learn more about each function.
PU_SUM
Calculates the sum of the specified source column for each element in the given target table.
Like the regular SUM operator, the column can either be an INT or FLOAT column. The data type of the result is the same as the input column data type.
PU_AVG
Calculates the average of the specified source column for each element in the given target table.
Like the regular AVG operator, the column can either be an INT or FLOAT column. The data type of the result is always a FLOAT.
PU_COUNT, PU_COUNT_DISTINCT
Calculates the (distinct) number of elements in the specified source column for each element in the given target table.
PU_COUNT and PU_COUNT_DISTINCT can be applied to any data type. The data type of the result is always an INT.
PU_MAX, PU_MIN
Calculates the maximum/minimum of the specified source column for each element in the given target table.
Like the regular MAX/MIN operator, PU_MAX/PU_MIN can be applied to any data type. The data type of the result is the same as the input column data type.
PU_MEDIAN
Calculates the median of the specified source column for each element in the given target table.
Like the regular MEDIAN operator, the column can either be an INT, FLOAT, or DATE column. The data type of the result is the same as the input column data type.
PU_QUANTILE
Calculates the quantile of the specified source column for each element in the given target table.
Like the regular QUANTILE operator, the column can either be an INT, FLOAT, or DATE column. The data type of the result is the same as the input column data type. The given quantile has to be a float number between 0 (same as PU_MIN) and 1.0 (same as PU_MAX).
PU_FIRST, PU_LAST
Returns the first/last element of the specified source column for each element in the given target table. An "ORDER BY" expression can be set as last parameter to define the order that should be used to determine the first/last element.
PU_FIRST, PU_LAST can be applied to any data type. The data type of the result is the same as the input column data type.
PU_STRING_AGG
Returns the concatenation of strings from the specified source column for each element in the given target table. The delimiter will be always inserted between the concatenation of the strings. Multiple order by expressions can be used in order to determine the order of the concatenation.
The PU_STRING_AGG function can only be applied to STRINGs.
Additionally, a filter condition can be passed to all PU-functions. You will learn more about this later in this course.
Example
As PU_FIRST and PU_LAST are quite new, let’s have a look at an example with a case and an activity table. You can use PU_FIRST and PU_LAST to identify the first and last activities for all your entries in the case table.
Assume you want to analyze where the process ends, or in other words, which activity was the last one conducted for every case.
To configure the PU-function we again need to think about three major things:
What is the aggregation type?
Which table is the target? As we want to return a result for every case, we take the Case table.
And lastly, which table is the source table, and which column are you interested in? As we want to pull the last activity, we specify the Activity table with the activity name column.
PU_LAST("Cases", "Activities"."Activity")
This function will now return the last activity for every case.
Examples for all PU-functions can be found in the PQL documentation.