我有一个看起来像这样的表:
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
我知道有一个summary运算符可以给我:
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。
预先感谢
答案 0 :(得分:0)
我找到了解决问题的方法,但是可能会有更好的方法。
mytable
| project id, timestamp, value1, value2
| order by timestamp desc
| summarize max(timestamp), makelist(value2) by id, value1
结果:
id timestamp value1 list_value2
1 09:12:42 1 ["2", "3", "1"]
2 09:33:15 3 ["2", "3", "1"]
现在您可以通过添加
扩展查询| project max_timestamp, id, value1, list_value2[0]
从该列表中获取第一个元素。用0到length(list_value2)-1之间的任何数字替换“ 0”以访问其他值。
另一个建议: 我使用的时间戳是ApplicationInsights生成的时间戳。在我们的代码中,我们调用TrackTrace记录一些数据。如果按此时间戳对行进行排序,则结果行列表不保证与代码中生成数据的顺序相同。
答案 1 :(得分:0)
如果我正确理解了您的问题,则您应该可以使用summarize arg_max()
:
doc: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 |