简体   繁体   中英

Match relationships between nodes with the same property

I've the following graph:

CREATE (a:Node)
CREATE (b:Node)
CREATE (c:Node)
CREATE (d:Node)

CREATE (a)-[:rel {referenceId: 1234, amount: 2}]->(b)
CREATE (b)-[:rel {referenceId: 1234, amount: 1}]->(c)
CREATE (b)-[:rel {referenceId: 1234, amount: 0.5}]->(d)

CREATE (a)-[:rel {referenceId: 4567, amount: 4}]->(b)
CREATE (b)-[:rel {referenceId: 4567, amount: 1}]->(c)
CREATE (b)-[:rel {referenceId: 4567, amount: 3}]->(d)

I'm looking for a way to calculate the difference between the amounts sent from a to b and from b to c/d depending on the referenceId but without using a specific referenceId.

So I'm looking for something like the semi-code below:

MATCH (a)-[in:rel]->(b)-[out:rel]->(c) WHERE in.referenceId == out.referenceId RETURN SUM(in.amount)-SUM(out.amount)

Is anyone having any idea how I can do this?

Maybe you need to do it per referenceId then?

  MATCH (a)-[in:rel]->(b)-[out:rel]->(c)
     WHERE in.referenceId = out.referenceId
  RETURN in.referenceId as referenceId, 
         SUM(in.amount)-SUM(out.amount) as diff

Result:

referenceId diff
4567        4
1234        2.5

You can also do it in a way so you look for non zero diff:

MATCH (a)-[in:rel]->(b)-[out:rel]->(c)
  WHERE in.referenceId = out.referenceId
WITH in.referenceId as referenceId,
       SUM(in.amount)-SUM(out.amount) as diff
WHERE diff <> 0
RETURN referenceId, diff

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM