传入&的密码总和属性外向关系(Neo4j) [英] Cypher sum property on incoming & outgoing relationships (Neo4j)

查看:82
本文介绍了传入&的密码总和属性外向关系(Neo4j)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个传入&节点(Neo4J)的传出关系.

I have multiple incoming & outgoing relationships for a node (Neo4J).

每个关系都有一个数字属性"weight"

Each relationship has a numeric property "weight"

我需要弄清传入的&节点的传出权重.

I need to get the difference between the incoming & outgoing weights for the node.

我尝试了此查询,但是即使输入权重之和为20,两个总和仍返回0.

I tried this query, but return 0 for both sums, even though sum of incoming weights is 20.

MATCH out=(n:Person)-[r:FRIEND]->(), in=(n:Person)<-[s:FRIEND]-() 
WHERE n.personid='12345' 
RETURN sum(r.fweight), sum(s.fweight);

此外,我尝试了此操作……它崩溃了/不返回了

Also, I tried this... and it crashes/doesnt return

MATCH (n:Person)-[r:FRIEND]->()  
MATCH (n:Person)<-[s:FRIEND]-()
WHERE n.personid='12345'
RETURN sum(r.fweight) , sum(s.fweight)

有任何线索吗??? :D

Any clue??? :D

推荐答案

可能是因为返回"中的属性名称"fweight"与关系"r"上实际使用的一个权重"不同或" s.如果将其更改为

It's probably because the property name "fweight" in your "Return" is not the same as the one "weight" that is actually used on the relationship "r" or "s". It should work if you change it to,

RETURN sum(r.weight), sum(s.weight)

但是结果是所有元组(r,s)的总和,其中将包括许多重复的r和重复的s.

But the result is the sum over all of the tuple (r, s) that would include many duplicate r, and duplicate s.

要获得确切的总和,您可以获取不同的r和s的集合,然后对这些集合求和,

To get the exact sum, you can get the collection of the distinct r, and s, then sum over the collections like this,

RETURN reduce(sumr = 0, x in collect(distinct r)|sumr + x.weight) as sumr, reduce(sums = 0, x in collect(distinct s)|sums + x.weight) as sums

显示查询的控制台具有"r"和"s"属性的直接总和,位于 http://console.neo4j.org/?id=cqs2h7

The console that shows the query with the direct sum over the properties of the "r" and "s" is here, http://console.neo4j.org/?id=cqs2h7

显示带有集合的查询的控制台在这里, http://console.neo4j.org/?id = 9st2mq

The console that shows the query with collection is here, http://console.neo4j.org/?id=9st2mq

您会注意到,尽管它们都返回了总和,但是第一个查询的结果为"RETURN sum(r.weight),sum(s.weight)"包括重复的关系"r"和"s",而第二个具有集合的查询将删除重复项并返回所需的总和.

You would notice that although both of them return the sums, the results of the first query with "RETURN sum(r.weight), sum(s.weight)" include the duplicated relationships "r" and "s" whereas the second query with collections removes the duplicates and returns the desired sum.

这篇关于传入&amp;的密码总和属性外向关系(Neo4j)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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