How to merge & sum up relationships

Is it possible to merge edges in Neo4j in such a way that the values of the properties add up?


Hello @marcelix161 :slight_smile:

Have a look at apoc.refactor.mergeRelationships() function in the APOC plugin. You can have more configuration here, (same as apoc.refactor.mergeNodes() function).


I deleted the previous post as it had an error. It did not work correctly when a node had multiple relationships to multiple nodes. I had to update the 'with' clause to include the nodes at each end of the relationships, so the grouping of the relationships was correct.

with n, m, sum(r.w) as total, collect(r) as relationships
where size(relationships) > 1
with total, head(relationships) as keep, tail(relationships) as delete
set keep.w = total
foreach(r in delete | delete r)

1 Like

Hi Gary,
Thank you for your solution! You've helped me a lot!