# 基本聚合函数

基本聚合函数是一组最常用的聚合函数。

使用每个函数的下拉列表可查看每个函数的简短描述和语法。单击语法描述中的函数名称可了解更多信息。

## 数据加载脚本中的基本聚合函数

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/AggregationFunctions/basic-aggregation-functions.htm)

#### FirstSortedValue

FirstSortedValue() 将返回来自 value 指定表达式的值，相当于 sort\_weight 参数排序的结果，例如，单价最低的产品名称。排序顺序中的第 n 个值，可在 rank 中指定。如果在指定 rank 下多个结果值共享同一 sort\_weight，则此函数返回 NULL。 排序的值会迭代于 group by 子句定义的大量记录，或者如果 group by 子句未定义，就会在整个数据集之间聚合。

[**firstsorted**](https://help.qlik.com/zh-CN/sense/May2025/Subsystems/Hub/Content/Sense_Hub/Scripting/AggregationFunctions/firstsorted.htm)([**firstsorted**](https://help.qlik.com/zh-CN/sense/May2025/Subsystems/Hub/Content/Sense_Hub/Scripting/AggregationFunctions/firstsorted.htm)**firstsorted**firstsorted([ distinctdistinct ] expression, sort\_weight [, rank ] ] expression, sort\_weight [, rank ]))

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/AggregationFunctions/basic-aggregation-functions.htm)

#### Max

Max() 用于查找表达式中聚合数据的最高数值，该数值由 group by 子句定义。通过指定 rank n，可以查找第 n 个最高值。

[**max**](https://help.qlik.com/zh-CN/sense/May2025/Subsystems/Hub/Content/Sense_Hub/Scripting/AggregationFunctions/max.htm)([**max**](https://help.qlik.com/zh-CN/sense/May2025/Subsystems/Hub/Content/Sense_Hub/Scripting/AggregationFunctions/max.htm)**max**max( expression[, rank] expression[, rank]))

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/AggregationFunctions/basic-aggregation-functions.htm)

#### Min

Min() 用于返回表达式中聚合数据的最低数值，该数值由 group by 子句定义。通过指定 rank n，可以查找第 n 个最低值。

[**min**](https://help.qlik.com/zh-CN/sense/May2025/Subsystems/Hub/Content/Sense_Hub/Scripting/AggregationFunctions/min.htm)([**min**](https://help.qlik.com/zh-CN/sense/May2025/Subsystems/Hub/Content/Sense_Hub/Scripting/AggregationFunctions/min.htm)**min**min( expression[, rank] expression[, rank]))

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/AggregationFunctions/basic-aggregation-functions.htm)

#### Mode

Mode() 用于返回表达式中聚合数据的最常出现的值（即模式值），该值由 group by 子句定义。Mode() 函数用于返回数字值和文本值。

[**mode**](https://help.qlik.com/zh-CN/sense/May2025/Subsystems/Hub/Content/Sense_Hub/Scripting/AggregationFunctions/mode.htm)([**mode**](https://help.qlik.com/zh-CN/sense/May2025/Subsystems/Hub/Content/Sense_Hub/Scripting/AggregationFunctions/mode.htm)**mode**mode(expression expression ))

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/AggregationFunctions/basic-aggregation-functions.htm)

#### Only

Only() 用于返回一个值（如果从聚合数据得出一个且只有一个可能值）。如果记录只包含一个值，则返回该值，否则返回 NULL 值。使用 group by 子句计算多个记录的值。Only() 函数用于返回数字值和文本值。

[**only**](https://help.qlik.com/zh-CN/sense/May2025/Subsystems/Hub/Content/Sense_Hub/Scripting/AggregationFunctions/only.htm)([**only**](https://help.qlik.com/zh-CN/sense/May2025/Subsystems/Hub/Content/Sense_Hub/Scripting/AggregationFunctions/only.htm)**only**only(expression expression ))

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/AggregationFunctions/basic-aggregation-functions.htm)

#### Sum

Sum() 用于计算表达式中聚合的值的总和，该总和由 group by 子句定义。

[**sum**](https://help.qlik.com/zh-CN/sense/May2025/Subsystems/Hub/Content/Sense_Hub/Scripting/AggregationFunctions/sum.htm)([distinct]expression)

## 图表表达式中的基本聚合函数

图表聚合函数只能在图表表达式的字段中使用。单个聚合函数的参数表达式不能包含其他聚合函数。

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/AggregationFunctions/basic-aggregation-functions.htm)

#### FirstSortedValue

FirstSortedValue() 将返回来自 value 指定表达式的值，相当于 sort\_weight 参数排序的结果，例如，单价最低的产品名称。排序顺序中的第 n 个值，可在 rank 中指定。如果在指定 rank 下多个结果值共享同一 sort\_weight，则此函数返回 NULL。

[**firstsortedvalue**](https://help.qlik.com/zh-CN/sense/May2025/Subsystems/Hub/Content/Sense_Hub/ChartFunctions/BasicAggregationFunctions/firstsortedvalue.htm)([{SetExpression}] [DISTINCT] [TOTAL [&lt;fld {,fld}&gt;]] value, sort\_weight [,rank])

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/AggregationFunctions/basic-aggregation-functions.htm)

#### Max

Max() 用于查找聚合数据的最高值。通过指定 rank n，可以查找第 n 个最高值。

[**max**](https://help.qlik.com/zh-CN/sense/May2025/Subsystems/Hub/Content/Sense_Hub/ChartFunctions/BasicAggregationFunctions/max.htm)([{SetExpression}] [DISTINCT] [TOTAL [&lt;fld {,fld}&gt;]] expr [,rank])

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/AggregationFunctions/basic-aggregation-functions.htm)

#### Min

Min() 用于查找聚合数据白最低值。通过指定 rank n，可以查找第 n 个最低值。

[**min**](https://help.qlik.com/zh-CN/sense/May2025/Subsystems/Hub/Content/Sense_Hub/ChartFunctions/BasicAggregationFunctions/min.htm)([{SetExpression}] [DISTINCT] [TOTAL [&lt;fld {,fld}&gt;]] expr [,rank])

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/AggregationFunctions/basic-aggregation-functions.htm)

#### Mode

Mode() 用于查找聚合数据的最常出现的值（即模式值）。Mode() 函数可处理文本值和数字值。

[**mode**](https://help.qlik.com/zh-CN/sense/May2025/Subsystems/Hub/Content/Sense_Hub/ChartFunctions/BasicAggregationFunctions/mode.htm) ({[SetExpression] [TOTAL [&lt;fld {,fld}&gt;]]} expr)

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/AggregationFunctions/basic-aggregation-functions.htm)

#### Only

Only() 用于返回一个值（如果从聚合数据得出一个且只有一个可能值）。例如，如果有多个产品的单价为 9，则只搜索单价为 9 的产品将会返回 NULL。

[**only**](https://help.qlik.com/zh-CN/sense/May2025/Subsystems/Hub/Content/Sense_Hub/ChartFunctions/BasicAggregationFunctions/only.htm)([{SetExpression}] [DISTINCT] [TOTAL [&lt;fld {,fld}&gt;]] expr)

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/AggregationFunctions/basic-aggregation-functions.htm)

#### Sum

Sum() 用于计算聚合数据之间表达式或字段指定值的总和。

[**sum**](https://help.qlik.com/zh-CN/sense/May2025/Subsystems/Hub/Content/Sense_Hub/ChartFunctions/BasicAggregationFunctions/sum.htm)([{SetExpression}] [DISTINCT] [TOTAL [&lt;fld {,fld}&gt;]] expr])