cancel
Showing results for 
Search instead for 
Did you mean: 

Aggregation operator result as property

I want to use aggregation operator to calculate certain value and then set this value to a node as property. Calculation works well and and I can return the value. When trying to set value I will get syntax error.

This query works OK:
MATCH (a:Object{category:'Personal'})-[r:BUILDS]->(b:Object{category:'Team'})
Return b.title AS team, SUM(a.achievement*r.weight/100) AS calculated

This query gives error:
MATCH (a:Object{category:'Personal'})-[r:BUILDS]->(b:Object{category:'Team'})
SET b.achievement = SUM(a.achievement*r.weight/100)
Return b.title AS team, b.achievement AS calculated

And error message is this: " Neo.ClientError.Statement.SyntaxError: Invalid use of aggregating function sum(...) in this context (line 3, column 21 (offset: 111))
"SET b.achievement = SUM(a.achievement*r.weight/100)" "

My question is how to use aggregation operator result as new property!

2 REPLIES 2

Hi there,

You'll want to perform the aggregation in a WITH clause (which is similar to a RETURN, but allows the query to continue, and also controls what remains in scope) and then use the SET clause to set the results of the aggregation:

MATCH (a:Object{category:'Personal'})-[r:BUILDS]->(b:Object{category:'Team'})
WITH b, SUM(a.achievement*r.weight/100) as calculated
SET b.achievement = calculated
RETURN b.title AS team, calculated

Thank you,

This is working and helping a lot!

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.