在单个Cypher查询中返回两个聚合? [英] Returning two aggregates in a single Cypher query?

查看:132
本文介绍了在单个Cypher查询中返回两个聚合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力与Cypher合作,以求取两个值的SUM并找出差异.我有以下两个查询,它们查找一个节点的发送总数和接收的总数:

I've been struggling some with Cypher in regards to taking the SUM of two values and finding the difference. I have these two queries, which find the total sent and the total received of a node:

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?我已经尝试过多个起点,如下所示:

Basically my question is - how do I combine these two separate queries so I can take the difference between Total_Sent and Total_Received? I have tried multiple start points like so:

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

But the Total_Received is null! To me this looks like a pretty simple use case - what the heck am I doing wrong?

推荐答案

仅通过像这样将两个查询粉碎在一起就不能合并两个查询. :)

You can't combine two queries by just smashing them together like that. :)

为解决此问题,建议您使用WITH将查询分为两部分,如下所示:

To solve this, I suggest you use WITH to separate your query into two parts, like this:

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,

安德烈斯(Andrés)

Andrés

这篇关于在单个Cypher查询中返回两个聚合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆