共用方式為


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() 至數值數據行會更有效率。
  • 不再支援 datetime 資料行的自動每小時間隔。 請改用明確的量化。 例如: 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. 您可能需要修改範例查詢中的資料表名稱,以符合工作區中的資料表。

下列範例會決定風暴造成直接傷害的唯一組合 StateEventType 組合。 沒有聚合函數,只要依索引鍵分組。 輸出只會顯示這些結果的數據行。

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 事件類型數目,並依唯一的 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
... ...

下列範例會計算直方圖 storm 事件類型,其 storm 持續時間超過 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

標準 count 函式在其計數中包含 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]