如何在Kusto中按多列分区获取每组Top3销售负责人数据?
解决Kusto按多列组合分组取TopN的问题
你当前的脚本问题在于partition仅按product单字段分区,导致结果跨billable_id和organization_id统计,不符合按三者组合分组的需求。以下两种方法可以实现目标:
方法一:多列作为partition分区键
Kusto的partition函数支持同时指定多个分区字段,用逗号分隔即可。直接将billable_id、organization_id、product都加入partition by的列表,就能确保每个组合分组内单独取Top3:
table_1 | where day >= ago(28d) | summarize hint.strategy=shuffle max_sales=max(sales_amount) by billable_id, organization_id, product, account_executive_id | partition hint.strategy=shuffle by billable_id, organization_id, product ( top 3 by max_sales desc )
方法二:使用窗口函数(更灵活)
如果需要更复杂的排名逻辑(比如处理并列排名),可以用row_number()/rank()/dense_rank()窗口函数,配合分区逻辑实现。如果数据量较大,先通过partition拆分再处理会更高效:
table_1 | where day >= ago(28d) | summarize max_sales=max(sales_amount) by billable_id, organization_id, product, account_executive_id | partition hint.strategy=shuffle by billable_id, organization_id, product ( extend rank = row_number() over (order by max_sales desc) | where rank <= 3 | project-away rank )
如果数据量不大,也可以省略partition直接在全局使用窗口函数:
table_1 | where day >= ago(28d) | summarize max_sales=max(sales_amount) by billable_id, organization_id, product, account_executive_id | extend rank = row_number() over (partition by billable_id, organization_id, product order by max_sales desc) | where rank <= 3 | project-away rank
内容的提问来源于stack exchange,提问作者SusanD
相关产品推荐
相关产品推荐

