# top-nested 运算符top-nested operator

``````T | top-nested 3 of Location with others="Others" by sum(MachinesNumber), top-nested 4 of bin(Timestamp,5m) by sum(MachinesNumber)
``````

`top-nested` 运算符接受表格数据作为输入，以及一个或多个聚合子句。The `top-nested` operator accepts tabular data as input, and one or more aggregation clauses. 第一个聚合子句（最左侧）根据针对这些记录的某个表达式的惟一值，将输入记录细分为多个分区。The first aggregation clause (left-most) subdivides the input records into partitions, according to the unique values of some expression over those records. 然后，子句会持有一定数量的记录，这可以最大化或最小化针对记录的这个表达式。The clause then keeps a certain number of records that maximize or minimize this expression over the records. 然后，下一个聚合子句以嵌套方式应用一个类似的函数。The next aggregation clause then applies a similar function, in a nested fashion. 以下每个子句都应用于前一个子句生成的分区。Each following clause is applied to the partition produced by the previous clause. 将持续为所有聚合子句执行此过程。This process continues for all aggregation clauses.

## 语法Syntax

T `|` `top-nested` TopNestedClause2 [`,` TopNestedClause2 ...]T `|` `top-nested` TopNestedClause2 [`,` TopNestedClause2 ...]

[ N ] `of` [ `ExprName` `=`] `Expr` [`with` `others` `=` `ConstExpr` ] `by` [ `AggName` `=`] `Aggregation` [`asc` | `desc`][ N ] `of` [ `ExprName` `=`] `Expr` [`with` `others` `=` `ConstExpr` ] `by` [ `AggName` `=`] `Aggregation` [`asc` | `desc`]

## 参数Arguments

• `N``long` 类型的文本，指示要为此层次结构级别返回多少前位值。`N` : A literal of type `long` indicating how many top values to return for this hierarchy level. 如果省略，将返回所有非重复值。If omitted, all distinct values will be returned.

• `ExprName` ：如果指定，则设置与 `Expr` 值相对应的输出列的名称。`ExprName` : If specified, sets the name of the output column corresponding to the values of `Expr` .

• `Expr` ：针对输入记录的表达式，指示要为此层次结构级别返回的值。`Expr` : An expression over the input record indicating which value to return for this hierarchy level. 通常它是表格式输入 (T) 的列引用，或针对此类列的某些计算（如 `bin()`）的列引用。Typically it's a column reference for the tabular input ( T ), or some calculation (such as `bin()`) over such a column.

• `ConstExpr` ：如果指定，对于每个层次结构级别，将添加 1 个记录，其值是所有未“使其成为前位值”的记录的聚合值。`ConstExpr` : If specified, for each hierarchy level, 1 record will be added with the value that is the aggregation over all records that didn't "make it to the top".

• `AggName` ：如果指定此标识符，则该标识符将在输出中为 Aggregation 的值设置列名称。`AggName` : If specified, this identifier sets the column name in the output for the value of Aggregation .

• `Aggregation` ：一个数值表达式，指示要应用于具有相同值 `Expr` 的所有记录的聚合。`Aggregation` : A numeric expression indicating the aggregation to apply to all records sharing the same value of `Expr` . 这个聚合的值决定了得到的记录中哪些排在“前位”。The value of this aggregation determines which of the resulting records are "top".

支持以下聚合函数：The following aggregation functions are supported:

• percentile(), and
• 还支持聚合的任何代数组合。Any algebraic combination of the aggregations is also supported.
• `asc``desc`（默认）可能会控制实际从聚合值范围“底部”还是“顶部”进行选择。`asc` or `desc` (the default) may appear to control whether selection is actually from the "bottom" or "top" of the range of aggregated values.

## 返回Returns

• 其中一列包含子句的 `Expr` 计算的非重复值（列名为 ExprName，如果指定了的话） One column holds the distinct values of the clause's `Expr` calculation (having the column name ExprName if specified)

• 其中一列保存 Aggregation 计算的结果（列名为 AggregationName，如果指定了的话） One column holds the result of the Aggregation calculation (having the column name AggregationName if specified)

## 说明Notes

• 省略 N 的值Omits the value of N
• 使用列名称作为 `Expr` 的值Uses the column name as the value of `Expr`
• 使用 `Ignore=max(1)` 作为该聚合，然后忽略（或 project-away）`Ignore` 列。Uses `Ignore=max(1)` as the aggregation, and then ignore (or project-away) the column `Ignore`.

## 示例Examples

``````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)
``````

KANSASKANSAS 87771.235500000187771.2355000001 执法机构Law Enforcement 18744.82318744.823 FT SCOTTFT SCOTT 264.858264.858
KANSASKANSAS 87771.235500000187771.2355000001 公用Public 22855.620622855.6206 BUCKLINBUCKLIN 488.2457488.2457
KANSASKANSAS 87771.235500000187771.2355000001 专业观测员Trained Spotter 21279.708321279.7083 SHARON SPGSSHARON SPGS 388.7404388.7404

Use the option 'with others':Use the option 'with others':

``````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)

``````

KANSASKANSAS 87771.235500000187771.2355000001 执法机构Law Enforcement 18744.82318744.823 FT SCOTTFT SCOTT 264.858264.858
KANSASKANSAS 87771.235500000187771.2355000001 公用Public 22855.620622855.6206 BUCKLINBUCKLIN 488.2457488.2457
KANSASKANSAS 87771.235500000187771.2355000001 专业观测员Trained Spotter 21279.708321279.7083 SHARON SPGSSHARON SPGS 388.7404388.7404

KANSASKANSAS 87771.235500000187771.2355000001 执法机构Law Enforcement 18744.82318744.823 所有其他结束位置All Other End Locations 18479.96518479.965
KANSASKANSAS 87771.235500000187771.2355000001 公用Public 22855.620622855.6206 所有其他结束位置All Other End Locations 22367.374922367.3749
KANSASKANSAS 87771.235500000187771.2355000001 专业观测员Trained Spotter 21279.708321279.7083 所有其他结束位置All Other End Locations 20890.967920890.9679

KANSASKANSAS 87771.235500000187771.2355000001 所有其他结束位置All Other End Locations 24891.083624891.0836

`````` StormEvents
| where State !in ('TEXAS', 'KANSAS')
| summarize sum(BeginLat)
``````
sum_BeginLatsum_BeginLat
1149279.59231149279.5923

``````StormEvents
| top-nested 2 of State by sum(BeginLat),    top-nested 2 of Source by sum(BeginLat),    top-nested 1 of EndLocation by sum(BeginLat), top-nested of EventType  by tmp = max(1)
| project-away tmp
``````

KANSASKANSAS 87771.235500000187771.2355000001 专业观测员Trained Spotter 21279.708321279.7083 SHARON SPGSSHARON SPGS 388.7404388.7404 雷雨大风Thunderstorm Wind
KANSASKANSAS 87771.235500000187771.2355000001 专业观测员Trained Spotter 21279.708321279.7083 SHARON SPGSSHARON SPGS 388.7404388.7404 冰雹Hail
KANSASKANSAS 87771.235500000187771.2355000001 专业观测员Trained Spotter 21279.708321279.7083 SHARON SPGSSHARON SPGS 388.7404388.7404 龙卷风Tornado
KANSASKANSAS 87771.235500000187771.2355000001 公用Public 22855.620622855.6206 BUCKLINBUCKLIN 488.2457488.2457 冰雹Hail
KANSASKANSAS 87771.235500000187771.2355000001 公用Public 22855.620622855.6206 BUCKLINBUCKLIN 488.2457488.2457 雷雨大风Thunderstorm Wind
KANSASKANSAS 87771.235500000187771.2355000001 公用Public 22855.620622855.6206 BUCKLINBUCKLIN 488.2457488.2457 洪水Flood

``````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)
| order 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
``````

KANSASKANSAS 专业观测员Trained Spotter SHARON SPGSSHARON SPGS 388.7404388.7404 00
KANSASKANSAS 专业观测员Trained Spotter ATWOODATWOOD 358.6136358.6136 11
KANSASKANSAS 专业观测员Trained Spotter LENORALENORA 317.0718317.0718 22
KANSASKANSAS 专业观测员Trained Spotter SCOTT CITYSCOTT CITY 307.84307.84 33
KANSASKANSAS 公用Public BUCKLINBUCKLIN 488.2457488.2457 00
KANSASKANSAS 公用Public ASHLANDASHLAND 446.4218446.4218 11
KANSASKANSAS 公用Public PROTECTIONPROTECTION 446.11446.11 22
KANSASKANSAS 公用Public MEADE STATE PARKMEADE STATE PARK 371.1371.1 33