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

如何用Kusto Query对比当前数据集与7天偏移数据并计算百分比差异

Kusto查询解决方案:对比24小时与7天前请求数差异

以下是满足需求的完整Kusto查询语句,会生成包含时间槽(HH:mm)、当前请求数、7天前请求数、差异百分比的结果表:

let currentB =
    requests
    | where Role == "tech"
    | where success == true
    | where name in ("POST /Auto", "POST Data/Play")
    | where timestamp >= ago(24h)  // 限定为过去24小时的数据
    | summarize current_count = count() by bin_timestamp = bin(timestamp, 5m)
    | extend time_slot = format_datetime(bin_timestamp, 'HH:mm');  // 转换为HH:mm格式的5分钟时间槽

let OffsetB =
    requests
    | where Role == "tech"
    | where success == true
    | where name in ("POST /Auto", "POST Data/Play")
    | where timestamp >= ago(8d) and timestamp < ago(7d)  // 对应7天前的同一24小时区间
    | summarize offset_count = count() by bin_timestamp = bin(timestamp, 5m)
    | extend time_slot = format_datetime(bin_timestamp, 'HH:mm');

// 关联两个数据集并计算差异百分比
currentB
| fullouter join OffsetB on time_slot
| project 
    time_slot,
    current_count = coalesce(current_count, 0),  // 无数据时填充0
    offset_count = coalesce(offset_count, 0),
    percentage_diff = iif(offset_count == 0, 
                          double(null),  // 避免除以0错误,偏移数据为0时显示null
                          round((current_count - offset_count) * 100.0 / offset_count, 2))  // 保留2位小数
| sort by time_slot

关键说明:

  • 时间对齐:通过format_datetime将5分钟粒度的时间转换为HH:mm格式,确保7天前的同一时间段与当前时间槽一一对应
  • 数据补全:使用fullouter join保留所有时间槽,同时用coalesce将缺失数据补为0,避免结果遗漏
  • 异常处理:通过iif判断7天前请求数为0的情况,避免除以0的计算错误
  • 排序:最终按时间槽排序,结果更符合时间顺序

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 07:27:19