你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

summarize operator

Applies to: ✅Microsoft FabricAzure Data ExplorerAzure MonitorMicrosoft Sentinel

生成可聚合输入表内容的表。

Syntax

T| summarize [ SummarizeParameters ] [[Column=] Aggregation [, ...]] [by [Column=] GroupExpression [, ...]]

Learn more about syntax conventions.

Parameters

Name 类型 Required Description
Column string 结果列的名称。 默认为派生自表达式的名称。
Aggregation string ✔️ A call to an aggregation function such as count() or avg(), with column names as arguments.
GroupExpression scalar ✔️ 一个可以引用输入数据的标量表达式。 输出包含任意数量的记录,因为所有组表达式都有不同的值。
SummarizeParameters string Zero or more space-separated parameters in the form of Name=Value that control the behavior. See supported parameters.

Note

When the input table is empty, the output depends on whether GroupExpression is used:

  • If GroupExpression isn't provided, the output is a single (empty) row.
  • If GroupExpression is provided, the output has no rows.

Supported parameters

Name Description
hint.num_partitions 指定用于在群集节点上共享查询负载的分区数。 See shuffle query
hint.shufflekey=<key> shufflekey 查询使用键将数据分区,在群集节点上共享查询负载。 See shuffle query
hint.strategy=shuffle shuffle 策略查询会在群集节点上共享查询负载,其中的每个节点将处理一个数据分区。 See shuffle query

Returns

输入行将排列成与 by 表达式具有相同值的组。 然后,对每个组计算指定的聚合函数,从而为每组生成行。 结果包含 by 列,还至少包含用于每个计算聚合的一列。 (某些聚合函数返回多个列。)

结果具有任意多行,因为值的不同组合 by (可能为零)。 如果未提供任何组键,则结果将包含单个记录。

若要基于数值范围进行汇总,请使用 bin() 将范围减小为离散值。

Note

  • 尽管可为聚合和分组表达式提供任意表达式,但使用简单列名称或将 bin() 应用于数值列会更加高效。
  • 不再支持日期/时间列的自动每小时箱。 请改用显式分箱。 例如 summarize by bin(timestamp, 1h)

聚合的默认值

下表汇总了聚合的默认值:

Operator Default value
count()、、countif()dcount()dcountif()count_distinct()sum()sumif()variance()、、varianceif()、、 stdev()stdevif() 0
make_bag()、、make_bag_if()make_list()make_list_if()make_set()make_set_if() 空的动态数组 ([])
All others null

Note

将这些聚合应用于包含 null 值的实体时,将忽略 null 值,并且不会将其纳入计算。 See Examples.

Examples

The examples in this article use publicly available tables in the help cluster, such as the StormEvents table in the Samples database.

The examples in this article use publicly available tables, such as the Weather table in the Weather analytics sample gallery. 可能需要修改示例查询中的表名称以匹配工作区中的表。

下面的示例确定导致直接受伤的风暴的唯一组合和State存在什么组合EventType。 没有聚合函数,只是有分组依据键。 输出仅显示这些结果的列。

StormEvents
| where InjuriesDirect > 0
| summarize by State, EventType

Output

下表仅显示了前 5 行。 若要查看完整输出,请运行查询。

State EventType
TEXAS Thunderstorm Wind
TEXAS Flash Flood
TEXAS Winter Weather
TEXAS High Wind
TEXAS Flood
... ...

以下示例查找夏威夷的最小和最大暴雨。 没有 group-by 子句,因此输出中只有一行。

StormEvents
| where State == "HAWAII" and EventType == "Heavy Rain"
| project Duration = EndTime - StartTime
| summarize Min = min(Duration), Max = max(Duration)

Output

Min Max
01:08:00 11:55:00

以下示例计算每个状态的唯一 storm 事件类型数,并按唯一风暴类型数对结果进行排序:

StormEvents
| summarize TypesOfStorms=dcount(EventType) by State
| sort by TypesOfStorms

Output

下表仅显示了前 5 行。 若要查看完整输出,请运行查询。

State TypesOfStorms
TEXAS 27
CALIFORNIA 26
PENNSYLVANIA 25
GEORGIA 24
ILLINOIS 23
... ...

以下示例计算持续时间超过 1 天的风暴的直方图风暴事件类型。 由于 Duration 有许多值,因此请使用 bin() 将它的值按 1 天的间隔分组。

StormEvents
| project EventType, Duration = EndTime - StartTime
| where Duration > 1d
| summarize EventCount=count() by EventType, Length=bin(Duration, 1d)
| sort by Length

Output

EventType Length EventCount
Drought 30.00:00:00 1646
Wildfire 30.00:00:00 11
Heat 30.00:00:00 14
Flood 30.00:00:00 20
Heavy Rain 29.00:00:00 42
... ... ...

以下示例显示输入表为空时的聚合的默认值。 运算符 summarize 用于计算聚合的默认值。 当 summarize 运算符的输入至少有一个空的分组依据键时,其结果也将为空。

如果 summarize 运算符的输入没有空的分组依据键,则结果将是在 summarize 中使用的聚合的默认值。有关详细信息,请参阅聚合的默认值

datatable(x:long)[]
| summarize any_x=take_any(x), arg_max_x=arg_max(x, *), arg_min_x=arg_min(x, *), avg(x), buildschema(todynamic(tostring(x))), max(x), min(x), percentile(x, 55), hll(x) ,stdev(x), sum(x), sumif(x, x > 0), tdigest(x), variance(x)

Output

any_x arg_max_x arg_min_x avg_x schema_x max_x min_x percentile_x_55 hll_x stdev_x sum_x sumif_x tdigest_x variance_x
NAN 0 0 0 0

avg_x(x) 的结果为 NaN,因为被除以 0。

datatable(x:long)[]
| summarize  count(x), countif(x > 0) , dcount(x), dcountif(x, x > 0)

Output

count_x countif_ dcount_x dcountif_x
0 0 0 0
datatable(x:long)[]
| summarize  make_set(x), make_list(x)

Output

set_x list_x
[] []

平均聚合只对非 null 值求和,并且只计算这些值,忽略任何 null 值。

range x from 1 to 4 step 1
| extend y = iff(x == 1, real(null), real(5))
| summarize sum(y), avg(y)

Output

sum_y avg_y
15 5

标准计数函数在其计数中包含 null 值:

range x from 1 to 2 step 1
| extend y = iff(x == 1, real(null), real(5))
| summarize count(y)

Output

count_y
2
range x from 1 to 2 step 1
| extend y = iff(x == 1, real(null), real(5))
| summarize make_set(y), make_set(y)

Output

set_y set_y1
[5.0] [5.0]