top-nested 運算子
top-nested
運算子會執行階層式匯總和值選取。
假設您有一個數據表,其中包含銷售資訊,例如區域、銷售人員及銷售金額。 操作員 top-nested
可協助您回答複雜的問題,例如「依銷售量列出前五個區域,以及這些區域中前三名銷售人員是誰?」
源數據會根據第一個 top-nested
子句中設定的準則進行分割,例如 region。 接下來,運算符會使用匯總來挑選每個分割區中的前幾筆記錄,例如新增銷售量。 每個後續 top-nested
子句會精簡先前子句所建立的數據分割,並建立更精確的群組階層。
結果是每個子句有兩個數據行的數據表。 一個數據行會保存數據分割值,例如區域,而另一個數據行則保存匯總計算的結果,例如銷售總和。
Syntax
T|
top-nested
[ N ] of
Expr [with
=
others
ConstExpr] Aggregation [asc
| desc
] by
[ ] [,
top-nested
... ]
深入瞭解 語法慣例。
參數
名稱 | 類型 | 必要 | Description |
---|---|---|---|
T | string |
✔️ | 輸入表格式表達式。 |
N | int |
要針對這個階層層級傳回的最上層值數目。 如果省略,則會傳回所有相異值。 | |
Expr | string |
✔️ | 輸入記錄上的表達式,指出要針對這個階層層級傳回的值。 一般而言,它會參考 T 的數據行,或涉及數據行上的 bin () 等計算。 選擇性地將輸出數據行名稱設定為 Name= Expr。 |
ConstExpr | string |
如果指定,則針對每個階層層級,會新增一筆記錄,其值為未將其設為頂端的所有記錄匯總。 | |
彙總 | string |
套用至具有相同 Expr 值的記錄的聚合函數。 結果會決定前幾個記錄。 請參閱 支援的聚合函數。 選擇性地將輸出數據行名稱設定為 名稱= 匯總。 |
支援的彙總函式。
支援下列彙總函式:
注意
也支援彙總的任何代數組合。
傳回
每個子句都有兩個數據行的數據表。 一個數據行包含使用 Expr 計算的唯一值,另一個數據行會顯示從 匯總 計算取得的結果。
包含來自其他數據行的數據
輸出數據表中只會顯示指定為 top-nested
Expr 子句的數據行。
若要在特定層級包含資料列的所有值:
- 請勿指定 N 的值。
- 使用數據行名稱作為 Expr的值。
- 使用
Ignore=max(1)
作為 匯總的值。 - 拿掉具有項目離開的不必要
Ignore
數據行。
如需範例,請參閱 取得每個狀態的最新事件,以及來自其他數據行的其他數據。
效能考量
記錄數目可能會隨著子句數目 top-nested
以指數方式成長,如果未指定 N 個限制,則記錄成長速度會更快。 此運算子可能會耗用大量的資源。
如果匯總的分佈相當不統一,請指定 N 來限制傳回的相異值數目。然後,使用 with
others
=
ConstExpr 規格取得所有其他案例權數的指示。
範例
開始使用 top-nested
運算子
下列查詢會 StormEvents
依數據行分割數據表, State
並計算每個狀態的總緯度。 查詢會選取具有最高緯度總和的前兩個狀態。 在這些前兩個狀態內,查詢會依據 分組 Source
數據,並選取具有最高緯度總和的前三個來源。 針對前兩個狀態中前三個來源的每個來源,查詢會依據 EndLocation
分組數據,並選取 EndLocation
具有最高緯度總和的 。
StormEvents // Data source.
| top-nested 2 of State by sum(BeginLat), // Top 2 States by total latitude.
top-nested 3 of Source by sum(BeginLat), // Top 3 Sources by total latitude in each State.
top-nested 1 of EndLocation by sum(BeginLat) // Top 1 EndLocation by total latitude in each Source and State.
輸出
State | aggregated_State | 來源 | aggregated_Source | EndLocation | aggregated_EndLocation |
---|---|---|---|---|---|
堪薩斯州 | 87771.2355000001 | 執法機關 | 18744.823 | FT SCOTT | 264.858 |
堪薩斯州 | 87771.2355000001 | 公開 | 22855.6206 | BUCKLIN | 488.2457 |
堪薩斯州 | 87771.2355000001 | Trained Spotter | 21279.7083 | SHARON SPGS | 388.7404 |
德克薩斯州 | 123400.5101 | 公開 | 13650.9079 | AMARILLO | 246.2598 |
德克薩斯州 | 123400.5101 | 執法機關 | 37228.5966 | PERRYTON | 289.3178 |
德克薩斯州 | 123400.5101 | Trained Spotter | 13997.7124 | CLAUDE | 421.44 |
使用另一個數據行的數據來增強最上層巢狀的結果
下列查詢會藉由引進額外的 top-nested
子句,以先前的範例為基礎。 在這個新子句中,缺少數值規格會導致擷取分割區中所有相異值 EventType
。 max(1)
匯總函式只是佔位元,呈現其結果無關,因此專案離開運算符會移除數據Ignore
行。 結果會顯示與先前匯總數據相關聯的所有事件類型。
StormEvents
| top-nested 2 of State by sum(BeginLat),
top-nested 3 of Source by sum(BeginLat),
top-nested 1 of EndLocation by sum(BeginLat),
top-nested of EventType by Ignore = max(1)
| project-away Ignore
輸出
State | aggregated_State | 來源 | aggregated_Source | EndLocation | aggregated_EndLocation | EventType |
---|---|---|---|---|---|---|
德克薩斯州 | 123400.51009999994 | 公開 | 13650.907900000002 | AMARILLO | 246.25979999999998 | Hail |
德克薩斯州 | 123400.51009999994 | 公開 | 13650.907900000002 | AMARILLO | 246.25979999999998 | Thunderstorm Wind |
堪薩斯州 | 87771.235500000068 | 公開 | 22855.6206 | BUCKLIN | 488.2457 | Flood |
堪薩斯州 | 87771.235500000068 | 公開 | 22855.6206 | BUCKLIN | 488.2457 | Thunderstorm Wind |
堪薩斯州 | 87771.235500000068 | 公開 | 22855.6206 | BUCKLIN | 488.2457 | Hail |
德克薩斯州 | 123400.51009999994 | Trained Spotter | 13997.712400000009 | CLAUDE | 421.44 | Hail |
堪薩斯州 | 87771.235500000068 | 執法機關 | 18744.823000000004 | FT SCOTT | 264.858 | Flash Flood |
堪薩斯州 | 87771.235500000068 | 執法機關 | 18744.823000000004 | FT SCOTT | 264.858 | Thunderstorm Wind |
堪薩斯州 | 87771.235500000068 | 執法機關 | 18744.823000000004 | FT SCOTT | 264.858 | Flood |
德克薩斯州 | 123400.51009999994 | 執法機關 | 37228.596599999961 | PERRYTON | 289.3178 | Hail |
... | ... | ... | ... | ... | ... |
用來 with
others
探索排除的數據
在 子句內 top-nested
併入時, with
others
規格引進額外的記錄,以匯總從最上層結果中排除的數據。 在下列查詢中 State
,會在和 aggregated_State
數據行中建立額外的記錄,代表除了關州和德州以外的所有州統一緯度。 此外, EndLocation
和 aggregated_EndLocation
數據行還有額外的九筆記錄。 這些記錄會顯示結束位置的結合緯度,而不符合每個狀態和來源內最上層位置的資格。
StormEvents
| top-nested 2 of State with others = "All Other States" by sum(BeginLat),
top-nested 3 of Source by sum(BeginLat),
top-nested 1 of EndLocation with others = "All Other End Locations" by sum(BeginLat)
輸出
State | aggregated_State | 來源 | aggregated_Source | EndLocation | aggregated_EndLocation |
---|---|---|---|---|---|
堪薩斯州 | 87771.2355000001 | 執法機關 | 18744.823 | FT SCOTT | 264.858 |
堪薩斯州 | 87771.2355000001 | 公開 | 22855.6206 | BUCKLIN | 488.2457 |
堪薩斯州 | 87771.2355000001 | Trained Spotter | 21279.7083 | SHARON SPGS | 388.7404 |
德克薩斯州 | 123400.5101 | 公開 | 13650.9079 | AMARILLO | 246.2598 |
德克薩斯州 | 123400.5101 | 執法機關 | 37228.5966 | PERRYTON | 289.3178 |
德克薩斯州 | 123400.5101 | Trained Spotter | 13997.7124 | CLAUDE | 421.44 |
堪薩斯州 | 87771.2355000001 | 執法機關 | 18744.823 | All Other End Locations | 18479.965 |
堪薩斯州 | 87771.2355000001 | 公開 | 22855.6206 | All Other End Locations | 22367.3749 |
堪薩斯州 | 87771.2355000001 | Trained Spotter | 21279.7083 | All Other End Locations | 20890.9679 |
德克薩斯州 | 123400.5101 | 公開 | 13650.9079 | All Other End Locations | 13404.6481 |
德克薩斯州 | 123400.5101 | 執法機關 | 37228.5966 | All Other End Locations | 36939.2788 |
德克薩斯州 | 123400.5101 | Trained Spotter | 13997.7124 | All Other End Locations | 13576.2724 |
堪薩斯州 | 87771.2355000001 | All Other End Locations | 24891.0836 | ||
德克薩斯州 | 123400.5101 | All Other End Locations | 58523.2932000001 | ||
All Other States | 1149279.5923 | All Other End Locations | 1149279.5923 |
下列查詢顯示上一個範例中所用第一個層級的相同結果。
StormEvents
| where State !in ('TEXAS', 'KANSAS')
| summarize sum(BeginLat)
輸出
sum_BeginLat |
---|
1149279.5923 |
排序階層式結果
為了達到完整的排序順序,下列查詢會針對每個群組的目前階層層級內每個值使用索引型排序。 此排序適用於根據最終巢狀層級排列結果,在此案例中為 EndLocation
。
StormEvents
| top-nested 2 of State by sum(BeginLat),
top-nested 2 of Source by sum(BeginLat),
top-nested 4 of EndLocation by sum(BeginLat)
| sort by State, Source, aggregated_EndLocation
| summarize
EndLocations = make_list(EndLocation, 10000),
endLocationSums = make_list(aggregated_EndLocation, 10000)
by State, Source
| extend indicies = range(0, array_length(EndLocations) - 1, 1)
| mv-expand EndLocations, endLocationSums, indicies
輸出
State | 來源 | EndLocations | endLocationSums | 索引 |
---|---|---|---|---|
德克薩斯州 | Trained Spotter | CLAUDE | 421.44 | 0 |
德克薩斯州 | Trained Spotter | AMARILLO | 316.8892 | 1 |
德克薩斯州 | Trained Spotter | DALHART | 252.6186 | 2 |
德克薩斯州 | Trained Spotter | PERRYTON | 216.7826 | 3 |
德克薩斯州 | 執法機關 | PERRYTON | 289.3178 | 0 |
德克薩斯州 | 執法機關 | LEAKEY | 267.9825 | 1 |
德克薩斯州 | 執法機關 | BRACKETTVILLE | 264.3483 | 2 |
德克薩斯州 | 執法機關 | GILMER | 261.9068 | 3 |
堪薩斯州 | Trained Spotter | SHARON SPGS | 388.7404 | 0 |
堪薩斯州 | Trained Spotter | ATWOOD | 358.6136 | 1 |
堪薩斯州 | Trained Spotter | LENORA | 317.0718 | 2 |
堪薩斯州 | Trained Spotter | SCOTT CITY | 307.84 | 3 |
堪薩斯州 | 公開 | BUCKLIN | 488.2457 | 0 |
堪薩斯州 | 公開 | ASHLAND | 446.4218 | 1 |
堪薩斯州 | 公開 | PROTECTION | 446.11 | 2 |
堪薩斯州 | 公開 | MEADE STATE PARK | 371.1 | 3 |
使用其他數據行的其他數據,取得每個狀態的最新事件
下列查詢示範如何擷取每個美國州的最新兩個事件,以及相關的事件詳細數據。 請注意,在特定數據行內使用 max(1)
,其 Ignore*
有助於透過查詢傳播數據,而不會造成任何選取邏輯。
StormEvents
| top-nested of State by Ignore0=max(1), // Partition the data by each unique value of state.
top-nested 2 of StartTime by Ignore1=max(StartTime), // Get the 2 most recent events in each state.
top-nested of EndTime by Ignore2=max(1), // Append the EndTime for each event.
top-nested of EpisodeId by Ignore3=max(1) // Append the EpisodeId for each event.
| project-away Ignore* // Remove the unnecessary aggregation columns.
| order by State asc, StartTime desc // Sort results alphabetically and chronologically.
從其他數據行取得每個身分識別的最新記錄
下列查詢示範如何擷取每個身分識別的最新記錄,並以上一個範例中引進的概念為基礎。 第一個 top-nested
子句會依的相異值 id
分割數據。 後續子句會根據 timestamp
每個的 來識別兩個 id
最近記錄。 其他資訊會與 top-nested
未指定的計數和任意 max(1)
匯總一起使用運算符附加。 最後,使用 project-away
運算子移除不必要的匯總數據行。
datatable(id: string, timestamp: datetime, otherInformation: string) // Create a source datatable.
[
"Barak", datetime(2015-01-01), "1",
"Barak", datetime(2016-01-01), "2",
"Barak", datetime(2017-01-20), "3",
"Donald", datetime(2017-01-20), "4",
"Donald", datetime(2017-01-18), "5",
"Donald", datetime(2017-01-19), "6"
]
| top-nested of id by Ignore0=max(1), // Partition the data by each unique value of id.
top-nested 2 of timestamp by Ignore1=max(timestamp), // Get the 2 most recent events for each state.
top-nested of otherInformation by Ignore2=max(1) // Append otherInformation for each event.
| project-away Ignore0, Ignore1, Ignore2 // Remove the unnecessary aggregation columns.
輸出
id | timestamp | otherInformation |
---|---|---|
巴拉克 | 2016-01-01T00:00:00Z | 2 |
唐納德 | 2017-01-19T00:00:00Z | 6 |
巴拉克 | 2017-01-20T00:00:00Z | 3 |
唐納德 | 2017-01-20T00:00:00Z | 4 |
相關內容
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應