graph – NEO4J – 查找节点的所有传入和传出边

我想找到所有传出边的数量与同一节点的所有传入边的数量之间的差异.节点是城市,关系是它们之间的转移.

我试过这样做:

MATCH ()-[i:TRANSFERS]->(n:City {name:"London"}),(n:City {name:"London"})-[o:TRANSFERS]->() 
RETURN distinct n.name, count(i) AS incoming, count(o) as outgoing, count(o)-count(i) AS difference
ORDER BY outgoing - incoming DESC

还有这个:

MATCH ()-[i:TRANSFERS]->(n:City {name:"London"})
OPTIONAL MATCH (n:City {name:"London"})-[o:TRANSFERS]->() 
RETURN distinct n.name, count(i) AS incoming, count(o) as outgoing, count(o)-count(i) AS difference
ORDER BY outgoing - incoming DESC

但他们似乎没有工作.任何的想法?

最佳答案 您可以在传入和传出连接的关系模式上使用大小:

MATCH (city:City {name:"London"})
WITH size((city)-[:TRANSFERS]->()) as out, 
     size((city)<-[:TRANSFERS]-()) as in,
     city
RETURN city, in, out, (out - in) as diff
ORDER BY diff DESC
点赞