3
votes

This query works:

FOR person IN 1..1 INBOUND @companyID employed_by
    LET age = DATE_DIFF(person.age * 1000, @currentTime * 1000, 'y')
    COLLECT label = age WITH COUNT INTO value
        RETURN {data: label, frequency: value}

And gives me this:

[
    {
        data: 18,
        frequency: 69
    },
    {
        data: 19,
        frequency: 73
    },
    {
        data: 20,
        frequency: 86
    }
]

But what i really want is something like this

{
    data: [18, 19, 20]
    frequency: [69, 73, 86]
}

I was expecting the following query to work - but the PUSH statements fail (syntax error), i tried a bunch of PUSH statements in FOR loops but can't get them to work as i expect, which would imply i am doing something absolutely mental

LET data = []
LET frequency = []
LET temp = 
        (
            FOR person IN 1..1 INBOUND @companyID employed_by
                LET age = DATE_DIFF(person.age * 1000, @currentTime * 1000, 'y')
                COLLECT label = age WITH COUNT INTO value
                    data = PUSH(data, label)
                    frequency = PUSH(frequency, value)
                    RETURN true
        )
RETURN {data: data, frequency: frequency}

Any advice would be great!

1
You figured it out, kudos! A note on PUSH: it lets you append an element, but you can't re-define an existing variable - what you tried is to change the variables data and frequency (multiple times), but it's not allowed (not side-effect free). AQL is not a full programming language, and this is on purpose: queries can be optimized a lot because of AQL's inherent limitations. There are usually pure AQL solutions though, using sub-queries for instance.CodeManX
since you found the answer to your problem already, can you mark your answer as accepted? ;-)dothebart

1 Answers

3
votes

And, after some more fiddling - this seems to be exactly what I needed:

LET temp = 
    (
        FOR person IN 1..1 INBOUND @companyID employed_by
            LET age = DATE_DIFF(person.age * 1000, @currentTime * 1000, 'y')
            COLLECT label = age WITH COUNT INTO value
                RETURN {data: label, frequency: value}
    )
RETURN {data: temp[*].data, frequency: temp[*].frequency}