gpt4 book ai didi

neo4j - 在单个 Cypher 查询中返回两个聚合?

转载 作者:行者123 更新时间:2023-12-05 01:14:54 25 4
gpt4 key购买 nike

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

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 为空!对我来说,这看起来是一个非常简单的用例 - 我到底做错了什么?

最佳答案

您不能通过像那样将两个查询拼凑在一起来组合两个查询。 :)

为了解决这个问题,我建议您使用 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

HTH,

安德烈斯

关于neo4j - 在单个 Cypher 查询中返回两个聚合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12269009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com