Filippo Grazioli
Filippo Grazioli

Reputation: 385

CYPHER query based on sum relationship property array

In my graph, relationships have a property named list. list is an array of int.

It looks like this:

(head:Node)-[r:RELATIONSHIP {list:...}]->(tail:Node)

I'd like to query all tails in the graph, where the sum of list is > 0.

Intuitively, I would do:

MATCH (:Node)-[r:RELATIONSHIP]->(tail:Node)
WHERE sum(r.list) > 0
RETURN tail

This does not work unfortunately. It throws an error. How can I write this query? Thanks

Upvotes: 0

Views: 62

Answers (2)

cybersam
cybersam

Reputation: 67044

This should work for you:

MATCH (:Node)-[r:RELATIONSHIP]->(tail:Node)
WHERE REDUCE(s = 0, v IN r.list | s + v) > 0
RETURN tail

Upvotes: 1

plastic
plastic

Reputation: 131

I think this will do what you want

MATCH p=(n1:Node)-[:RELATIONSHIP*2..2]->(n2:Node) with n1 as head, n2 as tail, REDUCE(totalScore = 0.0, r in relationships(p) | totalScore + toFloat(r.score)) as score where score>1.99 return head.name, tail.name, score

Notes: In my example I'm specifying a fixed path length of 2, so that I could check the total scores easily... If you graph has bidirectional relationships or loops, know that head=tail is possible...

Upvotes: 0

Related Questions