Как захватить текущий временной диапазон
Цель: я должен запросить таблицу 1 и объединиться с записями таблицы 2.
Примечание:-События в таблице 2 происходят перед таблицей 1, поэтому я хочу убедиться, что при запросе к таблице 2 startTime составляет менее 1 дня или нескольких часов.
однако кажется, что я не могу передать timecontext. Я пробовал несколько способов, но не смог.
let minTime = customEvents | summarize min(timestamp) by appId | project min_timestamp;
let startTime = toscalar(minTime);
customEvents
| where timestamp <= todatetime(startTime)
| take 10 ;
приведенный выше запрос, например, возвращает мне только самую первую запись в таблице.
1 ответ
Вы можете попробовать следующий подход:
let customEvents = datatable(appId: string, timestamp:datetime, message:string)
[
"A", datetime(2019-03-08 16:14:50), "Hello from 'A'",
"A", datetime(2019-03-08 15:14:50), "Hello from 'A'",
"A", datetime(2019-03-08 14:14:50), "Hello from 'A'",
"A", datetime(2019-03-08 13:14:50), "Hello from 'A'",
"B", datetime(2019-03-08 12:14:50), "Hello from 'B'",
"B", datetime(2019-03-08 11:14:50), "Hello from 'B'",
"D", datetime(2019-03-08 10:14:50), "Hello from 'D'",
"C", datetime(2019-03-08 09:14:50), "Hello from 'C'",
"D", datetime(2019-03-08 08:14:50), "Hello from 'D'",
"C", datetime(2019-03-08 07:14:50), "Hello from 'C'",
]
;
let minTimeByAppId = toscalar(
customEvents
| summarize min(timestamp) by appId
| summarize make_bag(pack(appId, min_timestamp)))
;
let otherTable = datatable(appId:string, timestamp:datetime, otherMessage:string)
[
"A", datetime(2019-03-08 16:14:50), "Another hello from 'A'",
"A", datetime(2019-03-07 16:14:50), "Another hello from 'A'",
"B", datetime(2019-03-07 16:14:50), "Another hello from 'B'",
"B", datetime(2019-03-08 16:14:50), "Another hello from 'B'",
"C", datetime(2019-03-08 16:14:50), "Another hello from 'C'",
"C", datetime(2019-03-08 16:14:50), "Another hello from 'C'",
"D", datetime(2019-03-09 16:14:50), "Another hello from 'D'",
"D", datetime(2019-03-08 16:14:50), "Another hello from 'D'",
]
;
otherTable
| where timestamp < minTimeByAppId[appId] // you could also add some 'delta' here
который выведет:
| appId | timestamp | otherMessage |
|-------|-----------------------------|------------------------|
| A | 2019-03-07 16:14:50.0000000 | Another hello from 'A' |
| B | 2019-03-07 16:14:50.0000000 | Another hello from 'B' |