聚合函数 aggregation-functions
聚合函数对一组值执行计算并返回单个汇总结果。 利用这些函数,您可以通过计算平均值、查找最小值和最大值、计数元素以及汇总数值来分析历程表达式中的数据。
当您需要执行以下操作时,请使用聚合函数:
- 从列表或数组中计算统计值(avg,sum,min,max)
- 对集合(count, countOnlyNull, countWithNull)中的元素进行计数,选项包括或排除null值
- 确定数据集中的唯一值(distinctCount,distinctCountWithNull)
- 根据计算指标做出数据驱动型决策
聚合函数会根据其特定行为自动处理空值,从而使处理可能包含缺失或未定义值的真实数据变得更轻松。
avg avg
返回一组表达式中的平均值,这些表达式以列表或两个表达式形式给定。 Null值将被忽略。
avg(<parameter>)支持的类型:
- listInteger
- listDecimal
- 小数
- 整数
avg(<listInteger>)
avg(<listDecimal>)
avg(<decimal>,<decimal>)
avg(<decimal>,<integer>)
avg(<integer>,<decimal>)
avg(<integer>,<integer>)
返回小数。
avg(@event{BarBeacon.inventory},5)
avg([10,3,8])
返回7.0。
avg(10.2, 3)
返回6.6。
count count
计算列表的元素,而不考虑null值。
count(<listAny>)
count(<listObject>)
| table 0-row-3 1-row-3 | ||
|---|---|---|
| 参数 | 类型 | 描述 |
| listToProcess | listString、listBoolean、listInteger、listDecimal、listDuration、listDateTime、listDateTimeOnly、listDateOnly或listObject | 要处理的列表。 对于listObject,它必须是字段引用。 listObject不能包含null对象。 |
count(<listAny>)
返回整数。
count([10,2,10,null])
返回3。
count(@event{my_event.productListItems})
返回给定对象数组(listObject类型)中的对象数。 备注: listObject不能包含null对象
countOnlyNull countOnlyNull
计算列表中的空值的数量。
countOnlyNull(<listAny>)| table 0-row-2 1-row-2 | |
|---|---|
| 参数 | 类型 |
| listToProcess | listString、listBoolean、listInteger、listDecimal、listDuration、listDateTime、listDateTimeOnly、listDateOnly |
countOnlyNull(<listAny>)
返回整数。
countOnlyNull([10,2,10,null])
返回1。
注意:此函数不支持参数<listObject>。
countWithNull countWithNull
计算列表的所有元素,包括null值。
countWithNull(<listAny>)| table 0-row-2 1-row-2 | |
|---|---|
| 参数 | 类型 |
| listToProcess | listString、listBoolean、listInteger、listDecimal、listDuration、listDateTime、listDateTimeOnly、listDateOnly |
countWithNull(<listAny>)
返回整数。
countWithNull([10,2,10,null])
返回4。
注意:此函数不支持参数<listObject>。
distinctCount distinctCount
计算不同值的数量,忽略空值。
distinctCount(<listAny>)| table 0-row-3 1-row-3 2-row-3 | ||
|---|---|---|
| 参数 | 类型 | 描述 |
| listToProcess | listString、listBoolean、listInteger、listDecimal、listDuration、listDateTime、listDateTimeOnly、listDateOnly或listObject | 要处理的列表。 对于listObject,它必须是字段引用。 |
| keyAttributeName | 字符串 | 此参数是可选的,并且仅适用于listObject。 如果未提供参数,则当所有属性都具有相同的值时,会将对象视为重复。 否则,如果给定的属性具有相同的值,则将对象视为重复。 |
distinctCount(<listAny>)
返回整数。
distinctCount(<listObject>)
distinctCount(<listObject>,<string>)
返回对象列表。
distinctCount([10,2,10,null])
返回2。
distinctCount(@event{my_event.productListItems})
返回给定对象数组(listObject类型)中完全不同的对象数。
distinctCount(@event{my_event.productListItems}, "SKU")
返回具有不同“SKU”属性值{}的对象数。
distinctCountWithNull distinctCountWithNull
计算不同值(包括空值)的数量。
distinctCountWithNull(<listAny>)| table 0-row-2 1-row-2 | |
|---|---|
| 参数 | 类型 |
| listToProcess | listString、listBoolean、listInteger、listDecimal、listDuration、listDateTime、listDateTimeOnly、listDateOnly |
distinctCountWithNull(<listAny>)
返回整数。
distinctCountWithNull([10,2,10,null])
返回3。
注意:此函数不支持参数<listObject>。
max max
返回一组表达式中的最大值,这些表达式以列表或两个表达式形式给定。 Null值将被忽略。
max(<parameter>)- listDuration
- listInteger
- listDecimal
- listDateTime
- listDateTimeOnly
- listDateOnly
- 持续时间
- 整数
- 小数
- dateTime
- dateTimeOnly
max(<listDuration>)
返回持续时间。
max(<listInteger>)
返回持续时间。
max(<listDateTimeOnly>)
返回不考虑时区的日期时间。
max(<listDateTime>)
返回日期时间。
max(<listDateOnly>)
返回日期。
max(<listDecimal>)
返回小数。
max(<decimal>,<decimal>)
返回小数。
max(<duration>,<duration>)
返回持续时间。
max(<dateTime>,<dateTime>)
返回日期时间。
max(<dateTimeOnly>,<dateTimeOnly>)
返回不考虑时区的日期时间。
max(<integer>,<integer>)
返回整数。
max(@event{BarBeacon.inventory},5)
max([10,3,8])
返回10。
max([10,null,8])
返回10。
min min
返回一组表达式中的最小值,这些表达式以列表或两个表达式形式给定。 Null值将被忽略。
min(<parameters>)- listDuration
- listInteger
- listDecimal
- listDateTime
- listDateTimeOnly
- listDateOnly
- 持续时间
- 整数
- 小数
- dateTime
- dateTimeOnly
min(<listDuration>)
返回持续时间。
min(<listInteger>)
返回持续时间。
min(<listDateTimeOnly>)
返回不考虑时区的日期时间。
min(<listDateTime>)
返回日期时间。
min(<listDateOnly>)
返回日期。
min(<listDecimal>)
返回小数。
min(<decimal>,<decimal>)
返回小数。
min(<duration>,<duration>)
返回持续时间。
min(<dateTime>,<dateTime>)
返回日期时间。
min(<dateTimeOnly>,<dateTimeOnly>)
返回不考虑时区的日期时间。
min(<integer>,<integer>)
返回整数。
min(@event{BarBeacon.inventory},5)
min([10,3,8])
返回3。
min([10,null,8])
返回8。
sum sum
返回一组表达式的值的总和。 Null值将被忽略。
sum(<parameters>)- listInteger
- listDecimal
- 持续时间
- 整数
- 小数
sum(<listDecimal>)
返回小数。
sum(<listInteger>)
返回整数。
sum(<integer>,<integer>)
返回整数。
sum(<decimal>,<decimal>)
返回小数。
sum(@event{BarBeacon.inventory},5)
sum([10,3,8])
返回21。
sum([10.5,null,8.1])
返回18.6。
This section contains structured knowledge intended to support interpretation, retrieval, and question answering related to this topic.
For complete understanding, this information should be combined with the documentation on this page. Neither source is intended to stand alone; the page describes the feature, while this section provides additional context that helps disambiguate terminology, intent, applicability, and constraints.
- TL;DR: This page documents all aggregation functions available in AJO journey expressions, covering how to compute averages, sums, min/max values, counts, and distinct counts over lists and arrays.
Intents:
- Calculate the average of a list of numeric values using
avg - Sum numeric values in a list or from event fields using
sum - Find the minimum or maximum value in a list using
minormax - Count non-null, null-only, or all elements in a list using
count,countOnlyNull, orcountWithNull - Count distinct values in a list, with or without nulls, using
distinctCountordistinctCountWithNull - Filter unique objects in a listObject by a specific key attribute using
distinctCountwith a key parameter
Glossary:
- listObject: A list of complex objects (field references); cannot contain null objects (product-specific)
- listAny: A list of any supported scalar type (string, boolean, integer, decimal, duration, dateTime, dateTimeOnly, dateOnly) (product-specific)
- Null value: An absent or undefined element in a list; most aggregation functions ignore nulls unless the function explicitly handles them (e.g.,
countOnlyNull,countWithNull,distinctCountWithNull)
Guardrails:
countOnlyNull,countWithNull, anddistinctCountWithNulldo not support the<listObject>parameter typedistinctCounton alistObjectrequires the list to be a field reference, not an inline literalcounton alistObjectrequires the list to be a field reference; a listObject cannot contain null objects
Terminology:
- Canonical name: Aggregation functions — Acronym: none — variants: aggregate functions, collection functions
- Synonyms: “count” = “count non-null elements”; “countWithNull” = “count all elements including nulls”
- Do not confuse: “distinctCount” (ignores nulls) ≠ “distinctCountWithNull” (includes nulls as a distinct value)
FAQ:
- Q: Does
avginclude null values in its calculation? — No,avgignores null values automatically. - Q: What is the difference between
countandcountWithNull? —countexcludes null values from the total, whilecountWithNullcounts every element including nulls. - Q: Can I use
countOnlyNullon a listObject? — No,<listObject>is not supported bycountOnlyNull,countWithNull, ordistinctCountWithNull. - Q: How do I count distinct objects in an array based on a specific attribute? — Use
distinctCount(@event{...}, "attributeName")providing the key attribute name as the second parameter. - Q: What does
maxreturn when the list contains nulls? —maxignores null values and returns the maximum among the non-null elements.