Kusto单查询内多次summarize不支持,如何实现需求无需分两次?
问题分析
原查询的核心问题是:第一个summarize执行后,结果集仅保留了orgid以及聚合生成的failedEvents、successEvents、p95EventTime字段,原始的name和cityid字段已被丢弃,因此第二个summarize无法引用这些字段,导致语法错误。
解决方案
不需要编写两个独立查询,有以下几种高效实现方式:
方式1:用fork拆分数据流并行处理
通过fork将过滤后的数据流拆分为两个分支,分别完成不同维度的聚合,最终输出两个结果表:
device_events | where orgid == 1 | fork ( // 分支1:按orgid统计失败/成功事件数、事件时间P95分位数 summarize failedEvents = countif(name=='failure'), successEvents = countif(name=='success'), p95EventTime = percentile(toreal(event_time), 95) by orgid ) ( // 分支2:按cityid统计fallback事件数 summarize fallbackCountByCity = countif(name=='fallback') by cityid )
方式2:缓存过滤数据后分别聚合(适合大数据量场景)
用materialize缓存过滤后的数据集,避免重复扫描原始表,再分别执行两个聚合逻辑:
let filtered_data = materialize(device_events | where orgid == 1); // 输出org维度统计结果 filtered_data | summarize failedEvents = countif(name=='failure'), successEvents = countif(name=='success'), p95EventTime = percentile(toreal(event_time), 95) by orgid; // 输出city维度统计结果 filtered_data | summarize fallbackCountByCity = countif(name=='fallback') by cityid;
方式3:合并结果到单表(可选)
如果需要把两种统计结果合并到同一个表中,可通过union并添加标识字段区分:
let filtered_data = device_events | where orgid == 1; union ( filtered_data | summarize failedEvents = countif(name=='failure'), successEvents = countif(name=='success'), p95EventTime = percentile(toreal(event_time), 95) by orgid | project orgid, metric_type = "org_level_summary", failedEvents, successEvents, p95EventTime ), ( filtered_data | summarize fallbackCountByCity = countif(name=='fallback') by cityid | project cityid, metric_type = "city_level_fallback", fallbackCountByCity )
内容的提问来源于stack exchange,提问作者voidMainReturn
相关产品推荐
相关产品推荐

