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

如何让KQL的make-series从非日起点开始后按完整日聚合?

KQL实现非自然日起始的混合时间序列聚合

你可以通过拆分区间分别处理再合并结果的方式实现需求:第一个区间从指定时间到次日零点,后续按完整自然日聚合。

解决方案代码

// 定义起始和结束时间变量,方便后续修改
let start_time = datetime("2023-09-28T13:44:15.0000");
let end_time = startofday(now());

// 处理第一个自定义区间:从start_time到次日0点
let first_segment = MyTable
| where id == 11111
| where timestamp between (start_time .. startofday(start_time + 1d))
| summarize avg_myValue = avg(myValue)
| extend timestamp = start_time;  // 时间戳设为起始点,匹配你示例中的格式

// 处理后续自然日区间:从次日0点到结束时间,按天聚合
let daily_segments = MyTable
| where id == 11111
| where timestamp between (startofday(start_time + 1d) .. end_time)
| summarize avg_myValue = avg(myValue) by bin(timestamp, 1d);  // bin函数自动对齐自然日起始点

// 合并两个部分并按时间排序
first_segment
| union daily_segments
| sort by timestamp asc;

代码说明

  1. 变量定义:先把起始时间和结束时间设为变量,后续调整时只需要修改这两行。
  2. 第一个区间处理:单独筛选时间范围到次日零点,聚合后将时间戳设为你指定的起始时间,和你示例中的输出格式一致。
  3. 自然日区间处理:使用bin(timestamp, 1d)函数,自动将时间对齐到自然日的起始点(零点),实现完整自然日的聚合。
  4. 合并排序:将两个部分的结果合并后按时间升序排列,得到符合要求的时间序列。

如果需要输出make-series生成的数组格式,可以用以下方式合并series数组:

let start_time = datetime("2023-09-28T13:44:15.0000");
let end_time = startofday(now());

let first_series = MyTable
| where id == 11111
| where timestamp between (start_time .. startofday(start_time + 1d))
| make-series avg_myValue = avg(myValue) on timestamp from start_time to start_time step 1d;

let daily_series = MyTable
| where id == 11111
| where timestamp between (startofday(start_time + 1d) .. end_time)
| make-series avg_myValue = avg(myValue) on timestamp from startofday(start_time + 1d) to end_time step 1d;

// 合并两个series的数组
first_series
| join kind=inner daily_series on $left.id == $right.id
| project 
    timestamp = array_concat($left.timestamp, $right.timestamp),
    avg_myValue = array_concat($left.avg_myValue, $right.avg_myValue)
| limit 1;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 12:04:56