聚合函数 aggregation-functions

聚合函数对一组值执行计算并返回单个汇总结果。 利用这些函数,您可以通过计算平均值、查找最小值和最大值、计数元素以及汇总数值来分析历程表达式中的数据。

当您需要执行以下操作时,请使用聚合函数:

聚合函数会根据其特定行为自动处理空值,从而使处理可能包含缺失或未定义值的真实数据变得更轻松。

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。

AI Knowledge Reference

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 min or max
  • Count non-null, null-only, or all elements in a list using count, countOnlyNull, or countWithNull
  • Count distinct values in a list, with or without nulls, using distinctCount or distinctCountWithNull
  • Filter unique objects in a listObject by a specific key attribute using distinctCount with 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, and distinctCountWithNull do not support the <listObject> parameter type
  • distinctCount on a listObject requires the list to be a field reference, not an inline literal
  • count on a listObject requires 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 avg include null values in its calculation? — No, avg ignores null values automatically.
  • Q: What is the difference between count and countWithNull? — count excludes null values from the total, while countWithNull counts every element including nulls.
  • Q: Can I use countOnlyNull on a listObject? — No, <listObject> is not supported by countOnlyNull, countWithNull, or distinctCountWithNull.
  • 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 max return when the list contains nulls? — max ignores null values and returns the maximum among the non-null elements.
recommendation-more-help
journey-optimizer-help