admin管理员组文章数量:1400844
Given a table with numeric columns A, X, and Y, I'd like to obtain an array of (X, Y) tuples for every value of A, as follows:
| A | Points |
|---|-------------------|
| 1 | [[1,3],[2,3],...] |
| 2 | [[3,1],[1,2],...] |
...
I attempted to do this with the following (query):
datatable(A:int, X:int, Y:int) [
1, 1, 3,
1, 2, 3,
2, 3, 1,
2, 1, 2
]
| summarize Points = make_list(pack_array(X, Y)) by A
However, it appears make_list flattens arrays (though I can't seem to find where this is documented), giving:
| A | Points |
|---|-----------|
| 1 | [1,3,2,3] |
| 2 | [3,1,1,2] |
There are of course alternatives, though none of them are particularly satisfying:
Pack the columns to a
dictionary
instead of anarray
:| summarize Points = make_list(bag_pack_columns(X, Y)) by A
[{"X":1,"Y":3},{"X":2,"Y":3}]
Put the
array
into adictionary
with just one key:| summarize Points = make_list(bag_pack("P", pack_array(X, Y))) by A
[{"P":[1,3]},{"P":[2,3]}]
Convert each packed array to
string
before aggregating:| summarize Points = make_list(tostring(pack_array(X, Y)) by A
["[1,3]","[2,3]"]
Encode the tuple as a (non-JSON) string before aggregating:
| summarize Points = make_list(strcat(X, ",", Y)) by A
["1,3","2,3"]
The ony way I've found to get to the desired end result of nested arrays is to construct the outer array as a JSON string, and then parse it to bring it back to an array
, which just... unpleasant:
| summarize Points = parse_json(strcat('[', strcat_array(make_list(tostring(pack_array(X, Y))), ","), ']')) by A
Is there a better way to achieve this?
(To be clear, all of the alternatives I mention work just fine for my purposes, at this point I'm mostly asking out of curiosity.)
Given a table with numeric columns A, X, and Y, I'd like to obtain an array of (X, Y) tuples for every value of A, as follows:
| A | Points |
|---|-------------------|
| 1 | [[1,3],[2,3],...] |
| 2 | [[3,1],[1,2],...] |
...
I attempted to do this with the following (query):
datatable(A:int, X:int, Y:int) [
1, 1, 3,
1, 2, 3,
2, 3, 1,
2, 1, 2
]
| summarize Points = make_list(pack_array(X, Y)) by A
However, it appears make_list flattens arrays (though I can't seem to find where this is documented), giving:
| A | Points |
|---|-----------|
| 1 | [1,3,2,3] |
| 2 | [3,1,1,2] |
There are of course alternatives, though none of them are particularly satisfying:
Pack the columns to a
dictionary
instead of anarray
:| summarize Points = make_list(bag_pack_columns(X, Y)) by A
[{"X":1,"Y":3},{"X":2,"Y":3}]
Put the
array
into adictionary
with just one key:| summarize Points = make_list(bag_pack("P", pack_array(X, Y))) by A
[{"P":[1,3]},{"P":[2,3]}]
Convert each packed array to
string
before aggregating:| summarize Points = make_list(tostring(pack_array(X, Y)) by A
["[1,3]","[2,3]"]
Encode the tuple as a (non-JSON) string before aggregating:
| summarize Points = make_list(strcat(X, ",", Y)) by A
["1,3","2,3"]
The ony way I've found to get to the desired end result of nested arrays is to construct the outer array as a JSON string, and then parse it to bring it back to an array
, which just... unpleasant:
| summarize Points = parse_json(strcat('[', strcat_array(make_list(tostring(pack_array(X, Y))), ","), ']')) by A
Is there a better way to achieve this?
(To be clear, all of the alternatives I mention work just fine for my purposes, at this point I'm mostly asking out of curiosity.)
Share Improve this question asked Mar 23 at 19:51 Philippe SignoretPhilippe Signoret 14.4k2 gold badges44 silver badges63 bronze badges1 Answer
Reset to default 1Add an additional nesting level by using nested pack_array
datatable(A:int, X:int, Y:int) [
1, 1, 3,
1, 2, 3,
2, 3, 1,
2, 1, 2
]
| summarize Points = make_list(pack_array(pack_array(X, Y))) by A
A | Points |
---|---|
1 | [[1,3],[2,3]] |
2 | [[3,1],[1,2]] |
本文标签: kqlAggregate with makelist without flattening arraysStack Overflow
版权声明:本文标题:kql - Aggregate with make_list without flattening arrays - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744271081a2598186.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论