You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何处理KQL查询空结果?Azure订阅资源计数异常解决

解决Azure订阅资源统计中无资源订阅计数错误的问题

你的问题出在左外连接后,无资源的订阅会生成一条资源字段为空的记录,count()会把这条空记录计入总数,导致无资源订阅被统计为1个资源。可以通过以下两种方式修正:

方法一:使用countif统计非空资源记录

resourceContainers 
    | where type =~ 'microsoft.resources/subscriptions'
    | project subscriptionId, subName=name
    | join kind=leftouter resources on subscriptionId
    | summarize nrOfResources=countif(isnotempty(resources.id)) by subscriptionId, subName
    | sort by nrOfResources asc

通过countif(isnotempty(resources.id))只统计存在资源ID的记录,无资源的订阅会返回0。

方法二:先标记资源存在状态,再求和

resourceContainers 
    | where type =~ 'microsoft.resources/subscriptions'
    | project subscriptionId, subName=name
    | join kind=leftouter resources on subscriptionId
    | extend hasResource = iff(isnotempty(resources.id), 1, 0)
    | summarize nrOfResources=sum(hasResource) by subscriptionId, subName
    | sort by nrOfResources asc

先通过extend给每条记录标记是否存在资源(1表示有,0表示无),再用sum汇总,无资源订阅的总和就是0。

关键说明

左外连接后,无资源的订阅会保留一条来自resourceContainers的记录,且resources表的所有字段为空。原来的count()会把这条空记录算入计数,而countif或sum的方式可以精准区分是否有实际资源存在。

内容的提问来源于stack exchange,提问作者nExoR LU

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 23:50:24