我有一张看起来像这样的表:
id timestamp value1 value2
1 09:12:37 1 1
1 09:12:42 1 2
1 09:12:41 1 3
1 10:52:16 2 4
1 10:52:18 2 5
2 09:33:12 3 1
2 09:33:15 3 2
2 09:33:13 3 3
我需要按 id 和 value1 分组。对于每个组,我想要具有最高时间戳的行。
上表的结果如下所示:
id timestamp value1 value2
1 09:12:42 1 2
2 09:33:15 3 2
我知道有一个汇总运算符会给我这个:
mytable
| project id, timestamp, value1, value2
| summarize max(timestamp) by id, value1
Result:
id timestamp value1
1 09:12:42 1
2 09:33:15 3
但是我也无法获得此行的 value2。
提前致谢
最佳答案
如果我正确理解您的问题,您应该可以使用 summarize arg_max()
:
文档:https://docs.microsoft.com/en-us/azure/kusto/query/arg-max-aggfunction
datatable(id:long, timestamp:datetime, value1:long, value2:long)
[
1, datetime(2019-03-20 09:12:37), 1, 1,
1, datetime(2019-03-20 09:12:42), 1, 2,
1, datetime(2019-03-20 09:12:41), 1, 3,
1, datetime(2019-03-20 10:52:16), 2, 4,
1, datetime(2019-03-20 10:52:18), 2, 5, // this has the latest timestamp for id == 1
2, datetime(2019-03-20 09:33:12), 3, 1,
2, datetime(2019-03-20 09:33:15), 3, 2, // this has the latest timestamp for id == 2
2, datetime(2019-03-20 09:33:13), 3, 3,
]
| summarize arg_max(timestamp, *) by id
这将导致:
| id | timestamp | value1 | value2 |
|----|-----------------------------|--------|--------|
| 2 | 2019-03-20 09:33:15.0000000 | 3 | 2 |
| 1 | 2019-03-20 10:52:18.0000000 | 2 | 5 |
关于azure-application-insights - Power M Query/Kusto 从组中取第一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55260787/