如何从AzureDataExplorer/Kusto中的子查询中引用外部查询以进行筛选+;延伸

如何从AzureDataExplorer/Kusto中的子查询中引用外部查询以进行筛选+;延伸,azure,azure-data-explorer,kql,Azure,Azure Data Explorer,Kql,我有两个表,都包含某个实体的运行状况事件。表1包含相隔几分钟的频繁事件。表2可能相隔几分钟到几个月。我想将表2到表1中的某个实体的最新运行状况事件关联起来 let Table1 = datatable (Table1Timestamp : datetime, Table1NameOfDataElement : string) [ datetime(2021-05-11 19:05:00), 'foo', datetime(2021-05-11 19:15:00), 'foo',

我有两个表,都包含某个实体的运行状况事件。表1包含相隔几分钟的频繁事件。表2可能相隔几分钟到几个月。我想将表2到表1中的某个实体的最新运行状况事件关联起来


let Table1 = datatable (Table1Timestamp : datetime, Table1NameOfDataElement : string)
[
    datetime(2021-05-11 19:05:00), 'foo',
    datetime(2021-05-11 19:15:00), 'foo',
    datetime(2021-05-11 19:12:00), 'foo',
    datetime(2021-05-11 19:09:00), 'bar',
    datetime(2021-05-11 19:15:00), 'bar',
];
let Table2 = datatable (Table2Timestamp : datetime, Table2NameOfDataElement : string, Other: int)
[
    // Data is ragged, days before first entry but is current state of 'foo'
    datetime(2021-05-09 19:05:00), 'foo', 1,
    datetime(2021-05-09 19:05:00), 'bar', 2,
    datetime(2021-05-11 19:09:00), 'bar', 3,
];
Table1
| where Table1Timestamp between (datetime(2021-05-11 19:00:00) .. datetime(2021-05-11 20:00:00))
// determine state of entity in table 1 every 10 minutes
| summarize arg_max(Table1Timestamp, *) by Table1NameOfDataElement, TimeInterval = bin(Table1Timestamp, 10minutes)
// Find the row in table2 that is closest in time proximity to the row from table 1
// This naive approach will not compile
| extend MostRecentColumnInOtherTable = toscalar(Table2 
                               | where Table2Timestamp < **Table1Timestamp** 
                               | where Table2NameOfDataElement == **Table1NameOfDataElement** 
                               | top 1 by Table2Timestamp | project Other)


设Table1=datatable(Table1Timestamp:datetime,Table1NameOfDataElement:string)
[
日期时间(2021-05-11 19:05:00),“foo”,
日期时间(2021-05-11 19:15:00),“foo”,
日期时间(2021-05-11 19:12:00),“foo”,
日期时间(2021-05-11 19:09:00),“酒吧”,
日期时间(2021-05-11 19:15:00),“酒吧”,
];
设Table2=datatable(Table2Timestamp:datetime,Table2NameOfDataElement:string,Other:int)
[
//数据不完整,在首次输入前几天,但当前状态为“foo”
日期时间(2021-05-09 19:05:00),'foo',1,
日期时间(2021-05-09 19:05:00),“酒吧”,2,
日期时间(2021-05-11 19:09:00),“酒吧”,3,
];
表1
|其中表1时间戳介于(日期时间(2021-05-11 19:00:00)…日期时间(2021-05-11 20:00:00))
//每10分钟确定表1中实体的状态
|按Table1NameOfDataElement汇总arg_max(Table1Timestamp,*),时间间隔=bin(Table1Timestamp,10分钟)
//查找表2中与表1中的行在时间上最接近的行
//这种幼稚的方法无法编译
|扩展MOSTRECENTCOLUMNINOTHERSTABLE=toscalar(表2
|其中Table2时间戳<**Table1时间戳**
|其中Table2NameOfDataElement==**Table1NameOfDataElement**
|表2前1时间戳|项目其他)
我不能这样做,因为kql不允许我在内部查询中引用外部查询中的列。我可以在实体名称上加入,但这只解决了一半问题,我需要在表1中查找事件之前的最后一个条目

一种简单的方法是在
bin(Table2Timestamp,10minutes)==bin(table1Timestamp,10minutes)
上加入,但表2中的数据可能离表1中的相关行有好几个月的距离,表2中只记录发生时的状态变化

表1的窗口很窄,通常包含数千行。表2可以追溯到几个月前,包含数十万行


如何快速查找表1中某行之前发生的表2中最后一次更改?

找到了适合我的方法。只需对实体名称进行连接,然后使用arg_min进行聚合,以找到时间戳差异最小化的行


let Table1 = datatable (Table1Timestamp : datetime, Table1NameOfDataElement : string)
[
    datetime(2021-05-11 19:05:00), 'foo',
    datetime(2021-05-11 19:15:00), 'foo',
    datetime(2021-05-11 19:12:00), 'foo',
    datetime(2021-05-11 19:09:00), 'bar',
    datetime(2021-05-11 19:15:00), 'bar',
];
let Table2 = datatable (Table2Timestamp : datetime, Table2NameOfDataElement : string, Other: int)
[
    // Data is ragged, days before first entry but is current state of 'foo'
    datetime(2021-05-09 19:05:00), 'foo', 1,
    datetime(2021-05-09 19:05:00), 'bar', 2,
    datetime(2021-05-11 19:09:00), 'bar', 3,
];
Table1
| where Table1Timestamp between (datetime(2021-05-11 19:00:00) .. datetime(2021-05-11 20:00:00))
// determine state of entity in table 1 every 10 minutes
| summarize arg_max(Table1Timestamp, *) by Table1NameOfDataElement, TimeInterval = bin(Table1Timestamp, 10minutes)
// Find the row in table2 that is closest in time proximity to the row from table 1
// This naive approach will not compile
| Lookup Table2 on $left.Table1NameOfDataElement == $right.Table1NameOfDataElement 
| where Table1Timestamp < Table2Timestamp
| summarize arg_min(TimeDiffFromTimestamp = datetime_diff('second', Table1Timestamp, Table2Timestamp), *) by bin(Table1TimeStamp), Table1NameOfDataElement 



设Table1=datatable(Table1Timestamp:datetime,Table1NameOfDataElement:string)
[
日期时间(2021-05-11 19:05:00),“foo”,
日期时间(2021-05-11 19:15:00),“foo”,
日期时间(2021-05-11 19:12:00),“foo”,
日期时间(2021-05-11 19:09:00),“酒吧”,
日期时间(2021-05-11 19:15:00),“酒吧”,
];
设Table2=datatable(Table2Timestamp:datetime,Table2NameOfDataElement:string,Other:int)
[
//数据不完整,在首次输入前几天,但当前状态为“foo”
日期时间(2021-05-09 19:05:00),'foo',1,
日期时间(2021-05-09 19:05:00),“酒吧”,2,
日期时间(2021-05-11 19:09:00),“酒吧”,3,
];
表1
|其中表1时间戳介于(日期时间(2021-05-11 19:00:00)…日期时间(2021-05-11 20:00:00))
//每10分钟确定表1中实体的状态
|按Table1NameOfDataElement汇总arg_max(Table1Timestamp,*),时间间隔=bin(Table1Timestamp,10分钟)
//查找表2中与表1中的行在时间上最接近的行
//这种幼稚的方法无法编译
|在$left.Table1NameOfDataElement==$right.Table1NameOfDataElement上查找表2
|其中Table1时间戳