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

如何将两条相同结构的KQL查询合并为一条?

合并KQL查询实现多维度统计结果同行展示

需求:将两条基于StorageBlobLogs表的统计查询合并为一条,在同一行展示RequesterObjectId分别为"gisa"和"BIS"的SftpWrite操作次数。

原始查询:

StorageBlobLogs 
| where Uri startswith "sftp://mystrgsftp.blob.core.windows.net/gisa/" 
and OperationName == "SftpWrite"
and RequesterObjectId  == "gisa"
and TimeGenerated between (startofday(now(),0) .. datetime(now))
| count as Count_Gisa;

StorageBlobLogs 
| where Uri startswith "sftp://mystrgsftp.blob.core.windows.net/gisa/" 
and OperationName == "SftpWrite"
and RequesterObjectId  == "BIS"
and TimeGenerated between (startofday(now(),0) .. datetime(now))
| count as Count_BIS;

可行方案

方案一:使用summarize结合case函数

先过滤所有共同条件,再通过case函数分别统计两个目标ID的操作次数:

StorageBlobLogs 
| where Uri startswith "sftp://mystrgsftp.blob.core.windows.net/gisa/" 
  and OperationName == "SftpWrite"
  and RequesterObjectId in ("gisa", "BIS")
  and TimeGenerated between (startofday(now()) .. now())
| summarize 
    Count_Gisa = sum(case(RequesterObjectId == "gisa", 1, 0)),
    Count_BIS = sum(case(RequesterObjectId == "BIS", 1, 0))

方案二:使用pivot转置结果

先按RequesterObjectId分组统计数量,再通过pivot将行转列为指定字段:

StorageBlobLogs 
| where Uri startswith "sftp://mystrgsftp.blob.core.windows.net/gisa/" 
  and OperationName == "SftpWrite"
  and RequesterObjectId in ("gisa", "BIS")
  and TimeGenerated between (startofday(now()) .. now())
| summarize count() by RequesterObjectId
| pivot RequesterObjectId, sum(count_)
| project Count_Gisa = gisa, Count_BIS = BIS

两种方案均会输出如下格式的结果:

Count_GisaCount_BIS
134245

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 07:45:05