Reputation:
I need to get the sum of properties of the relationships, from the returned list of a function like the next one
MATCH path=((house:Building {country: 'usa'})-[*]-(shelf:Building {country: 'mexico'}))
RETURN path;
The relationships can have differents names, but the propertys have the same keys(and different values), for example:
MERGE (house)-[:NAME1{num: '5'}]->(cabin)
MERGE (cabin)-[:NAME2{num: '10'}]->(shop)
MERGE (shop)-[:NAME1{num: '5'}]->(foo)
MERGE (foo)-[:NAME3{num: '30'}]->(shelf)
I need a query, applying the previous requirements, returning the sum of all relationship's properties.
How can I do it?
Upvotes: 1
Views: 744
Reputation: 67044
You can use the REDUCE function to calculate the total:
MATCH path=((house:Building {country: 'usa'})-[rels*]-(shelf:Building {country: 'mexico'}))
RETURN path, REDUCE(s = 0, r IN rels | s + TOINTEGER(r.num)) AS total;
NOTE: Ideally, you should store integer values for the num
property (instead of strings), so that you will not have to pay a performance penalty for converting all the string values to integers every time you need to use the numeric values. After you do that, you can remove the call to the TOINTEGER
function.
Upvotes: 3