how to take sum of column with same id neo4j

Viewed 135

I want to get the id of a user who has the sum of order count we have passed in the query.

Example:

consider this the below data

urid        oc
1           2
1           3
1           0
2           1
3           6
1           4
3           1

I want the following output

  1. When I pass total count as 9 it should return urid 1
  2. When I pass total count as 1 it should return urid 2
  3. When I pass total count as 7 it should return urid 3

I have tried with the following Cypher Query. It is not working as execpted.

MATCH (ur:UR { id: "arqz12a" })
WITH ur, SUM(ur.oc) as count
WHERE count = 9
WITH ur, count
RETURN count, ur.urid;
1 Answers

The reason is you are not selecting the urid but the node "ur" in line 2. Thus it will not sum up the total properly. Try below query.

MATCH (ur:UR )
WITH ur.urid as urid, SUM(ur.oc) as count
WHERE count = 9
RETURN count, urid;

To debug it, run below query and you will see that the sum is not working well without ur.urid on line 2.

MATCH (ur:UR { id: "arqz12a" })
WITH ur, SUM(ur.oc) as count
RETURN ur, count
Related