cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

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?

3X_d_a_da79d1e943c3762cb9ff76399e948e1bac036430.png

1 ACCEPTED SOLUTION

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.

match(n:Node)-[r:REL]->(m:Node) 
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)

View solution in original post

3 REPLIES 3

Hello @marcelix161

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

Regards,
Cobra

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.

match(n:Node)-[r:REL]->(m:Node) 
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)

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