I have experience with Neo4j and Cypher, but still struggle with aggregate functions. I'm trying to pull a CSV out of Neo4j that should look like this:
Location | Number of Node X at Location | Number of Node Y at Location |
---|---|---|
ABC | 5 | 20 |
DEF | 39 | 4 |
Etc. | # | # |
My current query looks like this:
MATCH (loc:Location)--(x:Node_X)
RETURN loc.key AS Location, count(x) AS `Number of Node X at Location`, 0 AS `Number of Node Y at Location`
UNION
MATCH (loc:Location)--(y:Node_Y)
RETURN loc.key AS Location, 0 AS `Number of Node X at Location`, count(y) AS `Number of Node Y at Location`
Which yields a table like:
Location | Number of Node X at Location | Number of Node Y at Location |
---|---|---|
ABC | 5 | 0 |
DEF | 39 | 0 |
Etc. | # | # |
ABC | 0 | 20 |
DEF | 0 | 4 |
Etc. | # | # |
I think I'm close, but I have double the number of Location rows as I need, and am not sure how to make the results more succinct. Suggestions on this and generally tips for aggregate functions are appreciated!
You can aggregate with distinct here.
The problem in accessing x and y in above query is it changes the cardinality of the solution. For each solution of x, it will have all the solutions of y. If you had n1 x nodes and n2 y nodes and you don't use distinct, then you would get n1*n2 nodes for each x and y.