pivot 外掛程式
將輸入數據表中一個數據行的唯一值轉換成輸出數據表中的多個數據行,並視需要對出現在最終輸出中的任何剩餘數據行值執行匯總,以輪替數據表。
注意
如果未指定 OutputSchema,外掛程式的 pivot
輸出架構會以輸入數據為基礎。 因此,使用不同數據輸入的外掛程式多次執行可能會產生不同的輸出架構。 這也表示參考未封裝數據行的查詢隨時可能會變成「中斷」。 基於這個理由,我們不建議將此外掛程式用於自動化作業,而不需指定 OutputSchema 函式。
Syntax
T | evaluate pivot(
pivotColumn[,
aggregationFunction] [,
column1[,
column2 ... ]])
[:
OutputSchema]
深入瞭解 語法慣例。
參數
名稱 | 類型 | 必要 | Description |
---|---|---|---|
pivotColumn | string |
✔️ | 要旋轉的數據行。 此數據列中的每個唯一值都會是輸出資料表中的數據行。 |
aggregationFunction | string |
匯總函數,用來將輸入數據表中的多個數據列匯總至輸出數據表中的單一數據列。 目前支援的函式:min() 、max() 、、take_any() 、dcount() sum() 、avg() 、stdev() 、 variance() count() make_list() make_bag() make_set() 。 預設值為 count() 。 |
|
column1, column2, ... | string |
數據行名稱或以逗號分隔的數據行名稱清單。 輸出資料表將在每個指定的資料行中包含一個額外的資料行。 預設值是樞紐數據行和匯總數據行以外的所有數據行。 | |
OutputSchema | 外掛程式輸出預期 pivot 數據行的名稱和類型。語法: ( ColumnName: ColumnType [, ...] ) 指定預期的架構,不需要先執行實際的查詢來探索架構,即可優化查詢執行。 如果運行時間架構不符合 OutputSchema 架構,就會引發錯誤。 |
傳回
Pivot 會傳回具有指定資料行的輪替的資料表 (column1、column2、...) 加上樞紐資料行的所有唯一值。 樞紐處理的資料行的每個資料格都會包含彙總函式計算。
範例
依資料行進行樞紐處理
針對以 『AL』 開頭的每個 EventType 和 State,計算此類型在此狀態中的事件數目。
StormEvents
| project State, EventType
| where State startswith "AL"
| where EventType has "Wind"
| evaluate pivot(State)
輸出
EventType | ALABAMA | ALASKA |
---|---|---|
Thunderstorm Wind | 352 | 1 |
High Wind | 0 | 95 |
Extreme Cold/Wind Chill | 0 | 10 |
Strong Wind | 22 | 0 |
使用彙總函式依資料行進行樞紐處理
針對以 『AR』 開頭的每個 EventType 和 State,顯示直接死結總數。
StormEvents
| where State startswith "AR"
| project State, EventType, DeathsDirect
| where DeathsDirect > 0
| evaluate pivot(State, sum(DeathsDirect))
輸出
EventType | ARKANSAS | ARIZONA |
---|---|---|
Heavy Rain | 1 | 0 |
Thunderstorm Wind | 1 | 0 |
Lightning | 0 | 1 |
Flash Flood | 0 | 6 |
Strong Wind | 1 | 0 |
Heat | 3 | 0 |
使用彙總函式和單一其他資料行依資料行進行樞紐處理
結果與上一個範例相同。
StormEvents
| where State startswith "AR"
| project State, EventType, DeathsDirect
| where DeathsDirect > 0
| evaluate pivot(State, sum(DeathsDirect), EventType)
輸出
EventType | ARKANSAS | ARIZONA |
---|---|---|
Heavy Rain | 1 | 0 |
Thunderstorm Wind | 1 | 0 |
Lightning | 0 | 1 |
Flash Flood | 0 | 6 |
Strong Wind | 1 | 0 |
Heat | 3 | 0 |
指定樞紐數據行、匯總函數和多個其他數據行
針對每個事件類型、來源和狀態,請加總直接死死的數目。
StormEvents
| where State startswith "AR"
| where DeathsDirect > 0
| evaluate pivot(State, sum(DeathsDirect), EventType, Source)
輸出
EventType | Source | ARKANSAS | ARIZONA |
---|---|---|---|
Heavy Rain | Emergency Manager | 1 | 0 |
Thunderstorm Wind | Emergency Manager | 1 | 0 |
Lightning | Newspaper | 0 | 1 |
Flash Flood | Trained Spotter | 0 | 2 |
Flash Flood | 廣播媒體 | 0 | 3 |
Flash Flood | Newspaper | 0 | 1 |
Strong Wind | 執法機關 | 1 | 0 |
Heat | Newspaper | 3 | 0 |
使用查詢定義的輸出架構進行樞紐分析
下列範例會選取 StormEvents 數據表中的特定數據行。 它會使用明確的架構定義,允許在執行實際查詢之前先評估各種優化。
StormEvents
| project State, EventType
| where EventType has "Wind"
| evaluate pivot(State): (EventType:string, ALABAMA:long, ALASKA:long)
輸出
EventType | ALABAMA | ALASKA |
---|---|---|
Thunderstorm Wind | 352 | 1 |
High Wind | 0 | 95 |
海洋雷風雨 | 0 | 0 |
Strong Wind | 22 | 0 |
Extreme Cold/Wind Chill | 0 | 10 |
冷/風冷 | 0 | 0 |
風力強風 | 0 | 0 |
風力偏高 | 0 | 0 |
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應