如何使用KQL按0.5分辨率将行拆分为含指定索引的多行?
用KQL实现按指定分辨率展开事件时间范围
现有原始数据:
| Row | Event | StartIndex | EndIndex |
|---|---|---|---|
| 1 | A | 0 | 3 |
| 2 | B | 1.9 | 2.7 |
需求是按0.5的分辨率,将每行数据根据StartIndex和EndIndex生成对应范围内的所有0.5间隔的Index行,最终输出如下:
| Row | Event | Index |
|---|---|---|
| 1 | A | 0 |
| 2 | A | 0.5 |
| 3 | A | 1 |
| 4 | A | 1.5 |
| 5 | A | 2 |
| 6 | A | 2.5 |
| 7 | A | 3 |
| 8 | B | 2 |
| 9 | B | 2.5 |
实现代码
// 模拟原始数据集 let original_data = datatable(Row:int, Event:string, StartIndex:double, EndIndex:double) [ 1, "A", 0, 3, 2, "B", 1.9, 2.7 ]; original_data // 生成覆盖目标范围的0.5步长序列,通过floor/ceil确保边界对齐 | extend IndexSeries = generate-series( floor(StartIndex / 0.5) * 0.5, ceil(EndIndex / 0.5) * 0.5, 0.5 ) // 将序列展开为单独行 | mv-expand Index = IndexSeries to typeof(double) // 过滤严格在原始起止范围内的数值 | where Index >= StartIndex and Index <= EndIndex // 整理列顺序并生成连续行号 | project Event, Index | order by Event, Index | extend Row = row_number() | project Row, Event, Index
关键逻辑说明
generate-series:生成从对齐后的起始值到对齐后的结束值、步长0.5的数值序列。用floor和ceil处理原始起止值,确保序列能覆盖到范围内所有符合0.5间隔的数值(比如B的1.9对齐到2.0,2.7对齐到2.5)。mv-expand:将每行的序列拆分为独立的行,每行对应一个Index值。where过滤:确保生成的Index严格在原始的StartIndex和EndIndex范围内,避免超出边界的数值。row_number():重新生成连续的行号,按事件和索引排序后得到预期的输出结构。
内容的提问来源于stack exchange,提问作者nrofis
相关产品推荐
相关产品推荐

