Kusto查询VM健康仪表盘场景下条件过滤统计问题求助
Kusto VM健康状态分类统计解决方案
你需要用extend算子新增分类标识列替代where过滤逻辑,where会剔除不符合条件的数据,而你需要保留所有符合基础过滤规则的VM进行归类统计,完整实现如下:
VM | where VMName has "XYZ" | where VMName !has "XYZ123" | where VMName has_any ("s","t","u","v") // 新增分类列,按规则给VM打标签 | extend StatusCategory = case( mode == "online" and enabled == true, "enabled", mode == "offline" and enabled == true, "disabled", "other" ) // 不需要统计其他类VM时可以打开下方注释 // | where StatusCategory != "other" // 统计各分类数量、总数、enabled占比 | summarize enabled_count = countif(StatusCategory == "enabled"), disabled_count = countif(StatusCategory == "disabled") | extend total_count = enabled_count + disabled_count | extend enabled_rate = round(todouble(enabled_count) / total_count * 100, 2) // 输出保留2位小数的百分比值
核心逻辑说明
case()是Kusto的多条件判断函数,支持按自定义规则匹配返回对应分类值countif()可以直接按条件统计对应分类的数量,无需额外分组操作- 计算占比时通过
todouble()先把整数转为浮点数,避免整数除法得到0的错误结果,round()的第二个参数可以按需调整百分比保留的小数位数
内容的提问来源于stack exchange,提问作者Milo_Dev
相关产品推荐
相关产品推荐

