我正在尝试返回一个关系属性(称为proportion
)加上Neo4j中Cypher查询匹配的所有关系的该属性的总和。我已经走到了这一步:
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;
返回:
+-----------------+
| reduced | total |
+-----------------+
| 2 | 2 |
| 1 | 1 |
+-----------------+
我期待的地方:
+-----------------+
| reduced | total |
+-----------------+
| 2 | 3 |
| 1 | 3 |
+-----------------+
作为Cypher的初学者用户,我不确定如何处理此查询;我显然没有正确使用reduce
。任何建议将不胜感激。
答案 0 :(得分:7)
您需要使用WITH
将查询分为两部分:
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