我正在努力实现类似SQL在KQL中的领先地位。SQL中的查询如下所示-
Select lead(changedBy,1,null) Over (partition by incidentId order by historyId) nextChangedBy
from IncidentHistory IH, Incidents I
where I.IncidentId = IH.IncidentId
and I.UpdatedDate > trunc(sysdate) -30;
有人能告诉我如何在Kusto查询语言中实现同样的功能吗?我在KQL中没有看到任何引导函数。
至少在这一点上,在windows函数的这一领域,KQL比SQL更重要
我们需要对数据进行分区并排序,然后我们可以使用next((,这与SQL lead((等效。
p.S.
请注意,在SQL中不需要lead(changedBy,1,null)
的详细语法
自1&null是默认值,lead(changedBy)
就足够了
话虽如此,如果需要,KQL确实有一个等效的语法。
let Incidents = datatable(incidentId:int, UpdatedDate:datetime)
[
1 ,datetime(2022-09-15 01:02:03)
,2 ,datetime(2022-10-01 04:05:06)
,3 ,datetime(2022-10-07 07:08:09)
];
let IncidentHistory = datatable(incidentId:int, historyId:int, changedBy:string)
[
1 , 10 , "CB1"
,2 , 20 , "CB2"
,2 , 50 , "CB3"
,2 , 60 , "CB4"
,2 , 80 , "CB5"
,3 , 30 , "CB6"
,3 , 40 , "CB7"
,3 , 70 , "CB8"
];
Incidents
| where UpdatedDate > startofday(ago(30d))
| join kind=inner IncidentHistory on incidentId
| partition hint.strategy=native by incidentId
(
order by historyId asc
| extend nextChangedBy = next(changedBy)
)
incidentId | 更新日期 | incidentId1 | 历史Id更改日期 | |||
---|---|---|---|---|---|---|
3 | 2022-10-07T07:08:09Z | 3 | 30 | CB6 | CB7||
3 | 2022-10-07T07:08:09Z | 3 | CB7 | CB8 | ||
3 | 2022-10-07T07:08:09Z | 3 | 70 | CB8|||
2 | 2022-10-01T04:05:06Z | 2 | 20 | CB2 | CB3||
2 | 2022-10-01T04:05:06Z | 2 | 50 | CB3 | CB4 | |
2 | 2022-10-01T04:05:06Z | 2 | 60 | CB4CB5|||
2 | 2022-10-01T04:05:06Z | 2 | 80 | CB5 |