how to get total score as a sum of two two score values in neo4j or cypher

374 Views Asked by At

I have tried the following query to get total score (score is in array of string like ("5","2")) as sum of home score and away score game.

match (e:Epl), (e1:Epl)
where ((e)-[:AWAY]->(e1) or  (e1)-[:HOME]->(e)) and e.home=e1.away
return e.home,e1.away,
sum(toInteger(e.score[0])+ toInteger(e1.score[1])) as totalScore

I do have the relationship between two nodes as below: enter image description here

I want to calculate total score for each team (sum of home score and away score)

1

There are 1 best solutions below

1
On

As an optimisation (to avoid creating a Cartesian product), it's beneficial to specify the -[:AWAY|HOME]- relationship, which allows either AWAY or HOME types in any direction. The WHERE clause then takes care of checking the direction of the relationship.

match (e:Epl)-[:AWAY|HOME]-(e1:Epl)
where ((e)-[:AWAY]->(e1) or (e1)-[:HOME]->(e))
  and e.home = e1.away
return
  sum(toInteger(e.score[0]) + toInteger(e1.score[1])) as totalScore