1

I am trying to use the SUM function and store the result of it as a new property of the relationship. But it is not working. The query I used is :

MATCH (a:Employee)-[r:CorporateMessage]->(b)
WHERE a.Eid = 6001 AND b.Eid IN [6002,6003,6004,6005,5001]
SET r.Internalsum = SUM(r.Count)

The error i got was:

Invalid use of aggregating function sum(...) in this context (line 1, column 124 (offset: 123)) "MATCH (a:Employee)-[r:CorporateMessage]->(b)WHERE a.Eid = 6001 AND b.Eid IN [6002,6003,6004,6005,5001] SET r.Internalsum = SUM(r.Count)"

Kindly explain what i am doing wrong.

Dave Bennett
  • 10,996
  • 3
  • 30
  • 41
Ram
  • 97
  • 12

1 Answers1

3

Try it:

MATCH (a:Employee)-[r:CorporateMessage]->(b)
WHERE a.Eid = 6001 AND b.Eid IN [6002,6003,6004,6005,5001]
WITH r, SUM(r.count) as count
SET r.Internalsum = count

Always put aggregation functions in WITH or RETURN.

Tezra
  • 8,463
  • 3
  • 31
  • 68
Bruno Peres
  • 15,845
  • 5
  • 53
  • 89
  • Thank you...but when i tried it I am getting this error: Variable `r` not defined (line 4, column 5 (offset: 135)) "SET r.Internalsum = count" – Ram May 09 '17 at 12:23
  • Hi! Edited the answer! :) I was forgot to pass `r` to the next context in the `WITH`. – Bruno Peres May 09 '17 at 12:25
  • Thank you dear!..it worked...could you explain why it didn't work with previous statement. – Ram May 09 '17 at 12:28
  • 2
    The `WITH` is used to chain queries, passing parts from the previous to the next query. In the previous statement I was forgot to add the `r` variable to the `WITH` clause. Thus `r` was undefined in the next context (context of `SET` in your case). It is clear? You can se the [WITH doc here](https://neo4j.com/docs/developer-manual/current/cypher/clauses/with/). – Bruno Peres May 09 '17 at 12:38