arangodbcollect

ArangoDB: Collect syntax statement error when adding the sum function for an collection attribute


I am trying to add all the Amounts in the edge collection and also extract the days from the date attribute in the edge collection named Transaction.

However, I am getting error in the collect statement.

for d in Transaction 
        filter d._to == "Account/123" 
        COLLECT aggregate ct =count(d._id),
        aggregate totamnt=sum(d.Amount), 
        aggregate daysactive= count(distinct date_trunc(d.Time))
return distinct {"Incoming Accounts":length, "Days Active": daysactive}

Solution

  • If I understand what you want to achieve correctly, this is a query to achieve it:

    FOR d IN Transaction
            FILTER d._to == "Account/123" 
            COLLECT AGGREGATE length = COUNT_UNIQUE(d._id), 
                              totamnt = SUM(d.Amount),
                              daysactive = COUNT_UNIQUE(DATE_TRUNC(d.Time, "day"))
            RETURN { 
                "Incoming Accounts": length , 
                "Days Active": LENGTH(daysactive), 
                "Total Amount": totamnt 
            }
    

    Note: The distinct is not necessary, I include the total amount in the return value, and specified "day" as the unit to truncate the date to.

    I tested this slightly adapted on a collection of mine and got sensible results.