0

我有一个节点(Neo4J)的多个传入和传出关系。

每个关系都有一个数字属性“权重”

我需要得到节点的传入和传出权重之间的差异。

我尝试了这个查询,但是对于两个总和都返回 0,即使传入权重的总和是 20。

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

另外,我试过这个......它崩溃/不返回

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

有什么线索???:D

4

2 回答 2

2

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)

But the result is the sum over all of the tuple (r, s) that would include many duplicate r, and duplicate 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

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

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

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.

于 2013-10-30T18:31:57.300 回答
1

在这里的例子(console.neo4j.org/?id=cqs2h7)你得到每个总和两次。

每一个进出都是一个独特的组合,所以你得到了每一个出入,所有这些都加在一起。

如果您将 RETURN 更改为,ID(r), ID(s), SUM(r.weight), SUM(s.weight)您将看到结果集包含每个 ID(s) 的每个 ID(r)。

你可以试试这个:

MATCH (n:Person)-[r:FRIEND]->() 
WHERE n.personid ='12345' 
WITH n, SUM(r.weight) AS sumR 
MATCH (n:Person)<-[s:FRIEND]-() 
RETURN sumR, SUM(s.weight)

现在你首先得到了 out rel 的 SUM。然后你得到 in rel 的总和。

于 2013-11-04T21:57:58.657 回答