0

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)

Suman
  • 53
  • 7
  • You mentioned what you have tried, but what is the problem with the results? What would you like to get as a result? – Gabor Szarnyas Jan 07 '18 at 09:23
  • 2
    From your image, it looks like for every outgoing :HOME relationship, there's an incoming :AWAY relationship. If this is so, then I'd recommend simplifying your model, and just using a single relationship, since you can infer the other easily. It would also simplify your query. If every outgoing :AWAY is paired with an incoming :HOME and vice versa, then you're performing quite a lot of redundant expansions that may be slowing your query. – InverseFalcon Jan 07 '18 at 09:37
  • Also, do arrays always have two elements? If so, it would make sense to use simple properties instead (e.g. `homeScore`, `awayScore`). – Gabor Szarnyas Jan 07 '18 at 09:43

1 Answers1

0

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
Gabor Szarnyas
  • 4,410
  • 3
  • 18
  • 42
  • Thank you for your guidance but the query you have suggested provides total scores of all teams. However, I have wanted to get a total score for each individual team. – Suman Jan 07 '18 at 09:31