Reputation: 322
I have a Neo4j instance and I'm using CYPHER to do the following:
I want to create a cumulative count query. I have the following structure:
Ideally I want an output like this:
Basically the purple nodes needs to count the blue-ish ones.
I have done a simple query that would count the number for each one, but how to accumulate all the results.
My sample query is:
MATCH (n:Field) WITH n
OPTIONAL MATCH (p)-[:RELATES_TO {predicate: 'has field'}]->(n) WITH n, p
RETURN n.my_id AS field_id, n.label AS field, COUNT(p) AS num
Help is appreciated.
Upvotes: 1
Views: 195
Reputation: 11216
Updated answer...
MATCH (n:Field)
OPTIONAL MATCH (n)-[:RELATES_TO*0..2 {predicate: 'has field'}]->(:Field)<-[:RELATES_TO {predicate: 'has field'}]-(p:Blue)
RETURN n.my_id AS field_id, n.label AS field, count(p) AS num
And you should identify the second label of the node you want to count (it might not be a Field
after all.
And you need to make the degrees away varaible but it should be constrained at some depth, for instance ..2
.
Find all the Field
that RELATE_TO
Blue
(made up that label, I did not see it in your example) at most two hops away and return the counts per field.
MATCH (n:Field)-[:RELATES_TO*..2]->(p:Blue)
RETURN n.my_id AS field_id, n.label AS field, count(p) AS num
Or if you want all of the fields even
MATCH (n:Field)
OPTIOANL MATCH (n)-[:RELATES_TO*..2]->(p:Blue)
RETURN n.my_id AS field_id, n.label AS field, count(p) AS num
Upvotes: 1