你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
top-nested 运算符
适用于:✅Microsoft Fabric✅Azure 数据资源管理器Azure Monitor✅Microsoft✅ Sentinel
top-nested
运算符将执行分层聚合和值选择。
假设你有一个表,其中包含区域、销售人员和销售金额等销售信息。 top-nested
运算符可以帮助你回答复杂的问题,例如“按销售额排名前五的区域是什么,以及每个区域中排名前三的销售人员是谁?”
源数据将基于第一个 top-nested
子句中设置的条件(例如区域)进行分区。 接下来,该运算符将使用聚合(如将销售额相加)来挑选每个分区中排名靠前的记录。 每个后续 top-nested
子句都会优化由上一个子句创建的分区,从而创建更精确的组的层次结构。
最终每个子句都可得到一个包含两列的表。 一列保存分区值(如区域),另一列保存聚合计算的结果,如总销售额。
语法
T |
top-nested
[ N ] of
Expr [with
others
=
ConstExpr] by
Aggregation [asc
| desc
] [,
top-nested
... ]
详细了解语法约定。
参数
客户 | 类型 | 必需 | 说明 |
---|---|---|---|
T | string |
✔️ | 输入表格表达式。 |
N | int |
要为此层次结构级别返回的排在前面的值的数目。 如果省略,将返回所有非重复值。 | |
Expr | string |
✔️ | 针对输入记录的表达式,指示要为此层次结构级别返回的值。 通常,它会引用来自 T 的列,或涉及一列上类似 bin() 的计算。 (可选)将输出列名称设置为 Name = Expr。 |
ConstExpr | string |
如果指定,则为每个层次结构级别添加一条记录,且该记录的值是所有未能排在前面的记录的聚合值。 | |
聚合 | string |
应用于具有相同 Expr 值的记录的聚合函数。 其结果将确定排名靠前的记录。 请参阅支持的聚合函数。 (可选)将输出列名称设置为 Name = Aggregation。 |
支持的聚合函数
支持以下聚合函数:
注意
还支持聚合的任何代数组合。
返回
每个子句都可得到一个包含两列的表。 一列包含使用 Expr 计算的唯一值,另一列显示通过 Aggregation 计算获得的结果。
with
others
使用子句
top-nested
使用运算符with
others
可增加查看在更广泛的数据集中上下文化的顶级内容的功能。 以这种方式评估数据在直观呈现数据时非常有用。
添加来自其他列的数据
输出表中仅显示指定为 top-nested
子句 Expr 的列。
若要包含特定级别的列的所有值:
- 请勿指定 N 的值。
- 使用列名称作为 Expr 的值。
- 使用
Ignore=max(1)
作为 Aggregation 的值。 - 使用 project-away 移除不必要的
Ignore
列。
有关示例,请参阅 具有其他列数据的每个状态的最新事件。
性能注意事项
记录数随子句数 top-nested
呈指数级增长,如果未 指定 N 参数,则记录增长甚至更快。 此运算符可以消耗大量资源。
如果聚合分布不规则,则通过指定 N 来限制要返回的非重复值数。然后,使用 with
others
=
ConstExpr 子句来了解所有其他事例的权重。
示例
按财产损坏的顶级损坏状态、事件类型和结束位置
以下查询按State
列对表进行分区StormEvents
,并计算每个状态的总属性损坏。 查询选择财产损失量最大的前两个状态。 在这两个状态中,查询按 EventType
数据分组,并选择最损坏的前三个事件类型。 然后,查询对 EndLocation
数据进行分组,并选择 EndLocation
具有最高损坏的查询。 结果中只显示一个 EndLocation
值,可能是由于风暴事件的大量性质或未记录结束位置。
StormEvents // Data source.
| top-nested 2 of State by sum(DamageProperty), // Top 2 States by total damaged property.
top-nested 3 of EventType by sum(DamageProperty), // Top 3 EventType by total damaged property for each State.
top-nested 1 of EndLocation by sum(DamageProperty) // Top 1 EndLocation by total damaged property for each EventType and State.
| project State, EventType, EndLocation, StateTotalDamage = aggregated_State, EventTypeTotalDamage = aggregated_EventType, EndLocationDamage = aggregated_EndLocation
输出
状态 | EventType | EndLocation | StateTotalDamage | EventTypeTotalDamage | EndLocationDamage |
---|---|---|---|---|---|
CALIFORNIA | 野火 | 1445937600 | 1326315000 | 1326315000 | |
CALIFORNIA | HighWind | 1445937600 | 61320000 | 61320000 | |
CALIFORNIA | DebrisFlow | 1445937600 | 48000000 | 48000000 | |
OKLAHOMA | IceStorm | 915470300 | 826000000 | 826000000 | |
OKLAHOMA | WinterStorm | 915470300 | 40027000 | 40027000 | |
OKLAHOMA | 洪水 | 商务 | 915470300 | 21485000 | 20000000 |
属性损坏with
others
分组的前五个州
以下示例使用top-nested
运算符来标识具有最高财产损失的前五个状态,并使用with
others
子句将损坏的属性分组为所有其他状态。 然后,它将前五个状态和所有其他状态的损坏属性可视化为 piechart
使用 render
命令。
StormEvents
| top-nested 5 of State with others="OtherStates" by sum(DamageProperty)
| render piechart
输出
具有其他列数据的每个状态的最新事件
以下查询使用相关事件详细信息检索每个美国州的最新两个事件。 它在某些列中使用 max(1)
数据来传播数据,而无需使用顶部嵌套选择逻辑。 使用 删除project-away
生成的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
提取每个标识的最新记录,并基于上一示例中介绍的概念。 第一个top-nested
子句按用作占位符的非重复值id
Ignore0=max(1)
对数据进行分区。 对于每个 id
记录,它基于 timestamp
这两条最新记录进行标识。 使用运算符追加 top-nested
其他信息,而无需指定计数并使用 Ignore2=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 |
---|---|---|
Barak | 2016-01-01T00:00:00Z | 2 |
Donald | 2017-01-19T00:00:00Z | 6 |
Barak | 2017-01-20T00:00:00Z | 3 |
Donald | 2017-01-20T00:00:00Z | 4 |