3

在获取两个值的总和并找到差异方面,我一直在与 Cypher 进行一些斗争。我有这两个查询,它们查找节点的发送总数和接收总数:

START addr = node(5)
MATCH addr <- [:owns] - owner - [to:transfers] -> receiver
RETURN SUM(to.value) AS Total_Sent

START addr = node(5)
MATCH addr <- [:owns] - owner <- [from:transfers] - sender
RETURN SUM(from.value) AS Total_Received

基本上我的问题是 - 我如何结合这两个单独的查询,以便我可以区分 Total_Sent 和 Total_Received?我尝试了多个起点,如下所示:

START sendAddr = node(5), receivedAddr = node(5)
MATCH sendAddr <- [:owns] - sendOwner - [to:transfers] -> receiver, receivedAddr <- [:owns] - receiveOwner <- [from:transfers] - sender
RETURN SUM(to.value) AS Total_Sent, SUM(from.value) AS Total_Received, SUM(to.value) - SUM(from.value) AS Balance

但 Total_Received 为空!对我来说,这看起来像是一个非常简单的用例——我到底做错了什么?

4

1 回答 1

5

您不能像这样将两个查询组合在一起。:)

为了解决这个问题,我建议您使用 WITH 将查询分成两部分,如下所示:

START addr = node(5)
MATCH addr <- [:owns] - owner - [to:transfers] -> receiver
WITH addr, SUM(to.value) AS Total_Sent

MATCH addr <- [:owns] - owner <- [from:transfers] - sender
WITH SUM(from.value) AS Total_Received, Total_Sent

RETURN Total_Received, Total_Sent, Total_Received - Total_Sent as Total_Balance

高温下,

安德烈斯

于 2012-09-04T19:03:01.663 回答