Cypher Query - Sum function for lower nodes

Hi Everyone,

I am facing a challenge calculating the sum of lower nodes in my graph.

Consider the below scenario:

Multiple such paths are present in the graph, and I want to calculate the sum of all the lowest nodes of Storage and store the value of Requirement in the second last node.

My query is this :slight_smile:

match p=(a:Production)-[]->(b:Storage)-[]->(c:Storage)-[]->(d:Storage)
with c, sum(d.Requirement) as req
set c.Requirement=req
return c;

So this is giving the correct result for the 1st graph that is 50 is getting stored in c.Requirement property of the first tree, but for the second one 100 is getting stored in c.Requirement, but I want that the value should not get doubled up even if 2 similar paths are there on that tree.

Any suggestions on how this situation can be handled ?

Thank You,
Pragya

using DISTINCT on the node, should do what you need

1 Like

Give this a try, it should also work for longer paths where you may not know the depth:

MATCH (a:Production)-[*]->(secondToLast:Storage)-->(last:Storage)
WHERE NOT (last)-->(:Storage)
WITH DISTINCT secondToLast, last
WITH secondToLast, sum(last.Requirement) as req
SET secondToLast.Requirement = req
1 Like