I have a collection of financial transactions where each transaction has a payer and a receiver. Suppose I want to compute an aggregate (for example, max) amount of operations between all pairs of payers and receivers.
I can collect total amount of operations for any given pair of payer and receiver with this query:
FOR op IN operations
COLLECT pan = op.data.PAYER_ACC_NUM, ran = op.data.RECEIVER_ACC_NUM
WITH COUNT
INTO cnt_op
RETURN obj = {
"cnt" : cnt_op
}
Which results in json:
[
{
"cnt": 2
},
{
"cnt": 3
},
{
"cnt": 3
},
etc...
Alternatively I can do:
RETURN cnt_op
Then output json will be:
[
2,
3,
3,
etc...
But I have not been able to move further with any of these. It’s been very cumbersome due to nesting in RETURNed data. It seems as if each returned value is disguised as an array, which makes any sort of aggregation of that data impossible. FLATTEN
does nothing at any depth parameter value.
Presumably for the same reason, iterating over this structure again:
FOR rec in to_array(cnt_op)
COLLECT
AGGREGATE max_cnt = max(rec.cnt_op)
RETURN max_cnt
Yields null
instead of the expected value (maximum value of all elements in an array).
How do I solve this issue?