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

使用Kusto KQL按ID填充字段空值为最近已知值

问题描述

现有一主表,其中ID为1、2的X列存在空值,表格如下:

IDDateTimeIngestionTimeXYZ
12012-12-28T12:04:002012-12-28T12:04:00121110
22012-12-28T12:06:002012-12-28T12:06:00297
32012-12-29T12:11:002012-12-29T12:11:00297
12012-12-29T12:15:002012-12-29T12:15:00337
22012-12-29T12:24:002012-12-29T12:24:0097

已定义函数demo(datetime:fromTime, datetime:toTime),查询时间范围为2012-12-29T12:11:00至当日,需将空值替换为对应ID的最近已知字段值,预期结果如下:

IDDateTimeIngestionTimeXYZ
12012-12-28T12:04:002012-12-28T12:04:00121110
22012-12-28T12:06:002012-12-28T12:06:00297
32012-12-29T12:11:002012-12-29T12:11:00297
12012-12-29T12:15:002012-12-29T12:15:00对应ID的最近已知值?337
22012-12-29T12:24:002012-12-29T12:24:00对应ID的最近已知值?97
解决方案

可以使用**向前填充(Forward Fill)**逻辑,结合按ID分组、时间排序的方式实现空值替换。以Kusto查询语言为例,通过partition by和prev()函数即可完成需求:

demo(datetime(2012-12-29T12:11:00), datetime(2012-12-29T23:59:59))
| partition by ID (
    order by DateTime asc
    | extend X = coalesce(X, prev(X))
    // 若存在连续空值,可重复调用prev()或使用scan运算符处理
)

核心逻辑说明

  • partition by ID:按ID分组,确保每个ID的填充值仅来自自身的历史数据
  • order by DateTime asc:按时间升序排列,保证取到的是最近的已知值
  • coalesce(X, prev(X)):若当前X为空,则取上一行的X值;若存在连续空值,可重复此步骤或使用scan运算符批量处理

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 20:35:26