如何用between运算符替代ON连接两个KQL表并解决报错
问题解决:KQL Join中使用Between运算符报错
报错原因
KQL的join运算符的on子句仅支持相等匹配规则:要么直接写匹配的列名(如on ProductID),要么写相等表达式(如$left.Col = $right.Col),不支持between这类范围比较条件。此外你的Table1中不存在Timestamp列,$left.Timestamp本身是无效引用,这也是潜在问题。
修正方案
根据需求分两种场景提供解决方案:
场景1:需要通过ProductID关联+时间范围过滤
如果Table1实际应包含Timestamp列,且需要基于时间范围筛选关联结果,需先通过ProductID完成关联,再用where子句添加时间范围条件:
let Table1 = datatable(ProductID:int,ProductName:string,Price:real, Timestamp:datetime) [ 1, "Laptop", 1000.0, datetime(2024-06-10T07:00:00Z), 2, "Smartphone", 500.0, datetime(2024-06-11T10:00:00Z), 3, "Tablet", 700.0, datetime(2024-06-11T11:00:00Z) ]; let Table2 = datatable(SaleID:int,ProductID:int,Timestamp:datetime) [ 101, 1, datetime(2024-06-10T08:00:00Z), 102, 2, datetime(2024-06-11T10:30:00Z), 103, 3, datetime(2024-06-11T11:45:00Z) ]; Table1 | join kind=inner Table2 on ProductID // 示例:筛选Table2的时间在Table1时间1小时范围内的记录 | where Table2.Timestamp between (Table1.Timestamp .. Table1.Timestamp + 1h) | project-away ProductID1
场景2:仅需通过ProductID关联
如果原意是仅通过ProductID关联两个表(原between条件为笔误),直接使用列名匹配即可:
let Table1 = datatable(ProductID:int,ProductName:string,Price:real) [ 1, "Laptop", 1000.0, 2, "Smartphone", 500.0, 3, "Tablet", 700.0 ]; let Table2 = datatable(SaleID:int,ProductID:int,Timestamp:datetime) [ 101, 1, datetime(2024-06-10T08:00:00Z), 102, 2, datetime(2024-06-11T10:30:00Z), 103, 3, datetime(2024-06-11T11:45:00Z) ]; Table1 | join kind=inner Table2 on ProductID | project-away ProductID1
内容的提问来源于stack exchange,提问作者Rasith
相关产品推荐
相关产品推荐

