如何在KQL中统计分组展开后列表内指定值的出现次数
Kusto查询:统计operationId在列表中的出现次数
需求说明
需要在执行mv-expand拆分operationIdSet(去重后的operationId集合)后,统计每个拆分出的operationId值,在对应的operationIdList(原始的operationId列表)中的出现次数,且必须保留mv-expand后的分组结构。
基础查询片段
| project endpoint, clientName, siteName, duration, operation_Id, customDimensions, operation_ParentId, id, target, operation_Name, itemType, client_City, itemCount, type, name, data | summarize NumberInGrouping = count(), operationIdSet = make_set(operation_Id), operationIdList = make_list(operation_Id) by endpoint, clientName, siteName | mv-expand operationIdSet to typeof(string)
问题场景
最初尝试使用countif直接计算,但该函数无法在extend上下文使用;后续尝试用summarize结合set_has_element,得到的结果始终为1,无法正确统计次数。
正确实现方案
使用filter函数筛选出列表中匹配的元素,再用array_length计算匹配元素的数量,直接通过extend生成统计结果:
| project endpoint, clientName, siteName, duration, operation_Id, customDimensions, operation_ParentId, id, target, operation_Name, itemType, client_City, itemCount, type, name, data | summarize NumberInGrouping = count(), operationIdSet = make_set(operation_Id), operationIdList = make_list(operation_Id) by endpoint, clientName, siteName | mv-expand operationIdSet to typeof(string) | extend some_num = array_length(filter(operationIdList, x => x == operationIdSet))
逻辑解释
filter(operationIdList, x => x == operationIdSet):遍历operationIdList,筛选出所有与当前operationIdSet值相等的元素,返回一个仅包含匹配项的新数组array_length(...):计算这个新数组的长度,也就是当前operationIdSet值在原始列表中的出现次数
内容的提问来源于stack exchange,提问作者mcool
相关产品推荐
相关产品推荐

