[英]Conditional sum in Neo4j Cypher query
I have a Neo4j database of bank accounts and their incoming transfers. 我有一个Neo4j数据库,其中包含银行帐户及其传入的转帐。 Each transfer occurs at some hour.
每次转移都会在某个小时发生。 There are 24 hourNodes representing each different hour (1,2,...,24).
有24个hourNodes代表每个不同的小时(1,2,...,24)。 I need to know the total money transferred to each account for each hour, like this (yes, I really need 25 columns in the resulting table!):
我需要知道每小时转移到每个帐户的总金额,是这样的(是的,我确实需要在结果表中显示25列!):
accountName totalH1 totalH2 ... totalH24 My Account #1 19 50 ... 34 My Account #2 5 14 ... 99 ...
This simple query: 这个简单的查询:
MATCH (account)->(transfer)->(hourNode)
RETURN
account.name,
hourNode.hour,
sum(transfer.amount)
gives results in a different format: 以不同的格式给出结果:
accountName hour total My Account #1 1 19 My Account #1 2 50 ... My Account #1 24 34 My Account #2 1 5 ...
I cannot change database structure. 我无法更改数据库结构。 How would I write a query to return the data in the format described?
如何编写查询以描述的格式返回数据?
Don't know a way how to exactly create the output you've described, but we can easily get very close to that. 不知道如何精确创建您描述的输出的方法,但是我们可以很容易地做到这一点。
What you basically want is a row per name and a ordered list of sums. 您基本上想要的是每个名称一行和一个有序的总和列表。 You can do that using a
WITH
that cares about ordering and a collect
to build the list: 您可以使用关注订购的
WITH
和建立列表的collect
来实现:
MATCH (account)->(transfer)->(hourNode)
WITH
account.name as name,
hourNode.hour as hour,
sum(transfer.amount) as sum
ORDER BY name, hour
RETURN name, collect(hour) as hours, collect(sum) as sums
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.