Returning sum of specific node property values Neo4j

Viewed 42

I have the same types of nodes in a Neo4j graph. I want to return node value sums. Below is an example graph:

enter image description here

I want to return them in such a way that node values (vals) are summed w.r.t alias and name fields.

[
{"name": "a", "val": 120},
{"name": "b", "val": 60}
]

The first object summed nodes with name value x, y, and a because x and y had an alias field with value a.


Update:

Indeed, val property in the nodes represents number of RELATED_TO relationships that a node has. I wanted to abstract that for simplifying the question. The real scheme is as shown below. Every white node has several RELATED_TO relationships to green nodes. How can I map the number of such relationships to terminal nodes (summing val fields)?

enter image description here

2 Answers

this should do it

MATCH (n)
RETURN COALESCE(n.alias, n.name) AS name, sum(n.val) AS val

Given your update you can count the relationships summed over alias or name by

MATCH (n)<-[rel:RELATED_TO]-()
RETURN COALESCE(n.alias, n.name) AS name, count(rel) AS cnt
Related