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.

Aggregate edges to parent

Given a Graph of children which each have a link to their parent, how can I perform an aggregation of these sub edges on the child level to a single edge at the top level which is simply the sum of the sub edges?

1 ACCEPTED SOLUTION

dkm1006
Node Clone

Assuming you have relationship types HAS and LINKS_TO and no grandchildren, the following Cypher query should work:


MATCH (p1)-[h1:HAS]->(c1)-[r:LINKS_TO]->(c2)<-[h2:HAS]-(p2)
WITH p1, p2, count(r) as linkCount
MERGE (p1)-[:DIRECT_LINK {strength: linkCount}]->(p2)

Before:


After:

View solution in original post

1 REPLY 1

dkm1006
Node Clone

Assuming you have relationship types HAS and LINKS_TO and no grandchildren, the following Cypher query should work:


MATCH (p1)-[h1:HAS]->(c1)-[r:LINKS_TO]->(c2)<-[h2:HAS]-(p2)
WITH p1, p2, count(r) as linkCount
MERGE (p1)-[:DIRECT_LINK {strength: linkCount}]->(p2)

Before:


After: