1
votes

I'm trying to return a relationship property (called proportion) plus the sum of that property for all relationship matched by a Cypher query in Neo4j. I've gotten this far:

START alice=node(3) 
MATCH p=(alice)<-[r:SUPPORTED_BY]-(n) 
RETURN reduce(total=0, rel in relationships(p): total + rel.proportion), sum(r.proportion) AS total;

This returns:

+-----------------+
| reduced | total |
+-----------------+
| 2       | 2     |
| 1       | 1     |
+-----------------+

where I was expecting:

+-----------------+
| reduced | total |
+-----------------+
| 2       | 3     |
| 1       | 3     |
+-----------------+

As a beginner user of Cypher, I'm not really sure how to approach this query; I'm clearly not using reduce correctly. Any advice would be appreciated.

1

1 Answers

7
votes

You need to use WITH to split up the query into two parts:

  1. find the sum of all proportions, and pass that as bound name to the next part
  2. find the individual proportions

.

START alice=node(3)
MATCH alice<-[r:SUPPORTED_BY]-() 
WITH sum(r.proportion) AS total 
MATCH alice<-[r:SUPPORTED_BY]-(other) 
RETURN other.name, r.proportion, total