Vinnie
Vinnie

Reputation: 302

Aggregating relationship properties on a path

I'm trying to to get the SUM of the weights on each path my MATCH finds. Query is below:

START n=node(10200) 
MATCH p=(n)-[r*1..5]->(m:Facility) 
WITH REDUCE(weights=0, rel IN r : weights + rel.weight) AS weight_sum 
WHERE ALL(n in nodes(p) WHERE 1=length(filter(m in nodes(p) : m=n))) 
RETURN p AS paths, length(p) AS pc,  
(weight_sum / (length(p) * (length(p) / 2))) AS sp;

Every time I run it, I'm getting...

Unknown identifier `p`

If I remove my WITH line (and the weight_sum RETURN value), the query knows what 'p' is and executes just fine. Is there a problem with my query that the value of 'p' is being lost? Is there a better alternative to get the SUM of these relationship properties?

Upvotes: 1

Views: 1103

Answers (1)

Luanne
Luanne

Reputation: 19373

You can just pipe "p" to the next part of the query via the WITH:

START n=node(10200) 
MATCH p=(n)-[r*1..5]->(m:Facility) 
WITH REDUCE(weights=0, rel IN r : weights + rel.weight) AS weight_sum, p
WHERE ALL(n in nodes(p) WHERE 1=length(filter(m in nodes(p) : m=n))) 
RETURN p AS paths, length(p) AS pc,  
(weight_sum / (length(p) * (length(p) / 2))) AS sp;

Upvotes: 4

Related Questions