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?

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:

1 Like