共用方式為


summarize 運算子

適用於:✅Microsoft網狀架構✅✅✅

產生匯總輸入數據表內容的數據表。

語法

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

深入瞭解 語法慣例

參數

姓名 類型​​ 必要 描述
資料行 string 結果數據行的名稱。 預設為衍生自表達式的名稱。
彙總 string ✔️ 呼叫 聚合函數 ,例如 count()avg(),且數據行名稱為自變數。
GroupExpression 純量 ✔️ 可參考輸入數據的純量表達式。 輸出的記錄數目會與所有群組表達式有相異的值一樣多。
SummarizeParameters string 以名稱=的形式控制行為之零個或多個空格分隔參數。 請參閱 支持的參數

注意

當輸入數據表是空的時,輸出取決於是否 使用 GroupExpression

  • 如果未 提供 GroupExpression ,則輸出會是單一(空白)數據列。
  • 如果 提供 GroupExpression ,輸出就不會有任何數據列。

支援的參數

姓名 描述
hint.num_partitions 指定用來共用叢集節點上查詢負載的數據分割數目。 請參閱 隨機查詢
hint.shufflekey=<key> 查詢 shufflekey 會使用索引鍵來分割數據,在叢集節點上共用查詢負載。 請參閱 隨機查詢
hint.strategy=shuffle 策略 shuffle 查詢會在叢集節點上共用查詢負載,其中每個節點都會處理一個數據分割。 請參閱 隨機查詢

傳回

輸入數據列會排列成具有相同表達式值的 by 群組。 然後會針對每個群組計算指定的聚合函數,為每個群組產生一個數據列。 結果包含數據行 by ,也包含每個計算匯總至少一個數據行。 (某些聚合函數會傳回多個數據行。

結果的數據列數目與值的不同組合 by 一樣多(可能為零)。 如果沒有提供群組索引鍵,結果就會有單一記錄。

若要摘要說明數值範圍,請使用 bin() 來將範圍縮減為離散值。

注意

  • 雖然您可以同時為匯總和群組表達式提供任意表達式,但使用簡單數據行名稱或套用 bin() 至數值數據行會更有效率。
  • 不再支援 datetime 資料行的自動每小時間隔。 請改用明確的量化。 例如: summarize by bin(timestamp, 1h)

匯總的預設值

下表摘要說明匯總的預設值:

Operator 預設值
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

注意

將這些匯總套用至包含 Null 值的實體時,會忽略 Null 值,且不會納入計算。 如需範例,請參閱 匯總預設值

範例

本節中的範例示範如何使用 語法來協助您開始使用。

本文中的範例會使用 說明叢集中公開可用的數據表,例如 Samples 資料庫中的 StormEvents 數據表。

本文中的範例會使用公開可用的數據表,例如天氣分析中的 StormEvents 數據表,範例數據

依水果和供應商摘要價格。

唯一組合

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

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

輸出

下表僅顯示前 5 個數據列。 若要查看完整的輸出,請執行查詢。

州/省 EventType
德克薩斯州 雷暴風
德克薩斯州 暴洪
德克薩斯州 冬季天氣
德克薩斯州 強風
德克薩斯州 洪水
... ...

時間戳下限和最大值

在夏威夷發現最低和最大的暴雨暴雨。 沒有 group-by 子句,因此輸出中只有一個數據列。

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

輸出

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

相異計數

下列查詢會計算每個狀態的唯一 storm 事件類型數目,並依唯一的 Storm 類型數目來排序結果:

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

輸出

下表僅顯示前 5 個數據列。 若要查看完整的輸出,請執行查詢。

州/省 TypesOfStorms
德克薩斯州 27
加利福尼亞州 26
賓夕法尼亞州 25
喬治亞州 24
伊利諾州 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

輸出

EventType 長度 EventCount
乾旱 30.00:00:00 1646
野火 30.00:00:00 11
熱度 30.00:00:00 14
洪水 30.00:00:00 20
大雨 29.00:00:00 42
... ... ...

匯總預設值

當運算子的 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)

輸出

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)

輸出

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

輸出

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)

輸出

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)

輸出

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)

輸出

set_y set_y1
[5.0] [5.0]