Akash Kumar
Akash Kumar

Reputation: 642

Getting count of branch node of multiple root nodes in Neo4J using Cypher Query

enter image description here

As shown in image,
3 root node - A,B,C - likewise D,E,F etc
6 Branch Node - A1,a2,a3,b1,b2,c1
I want to get the count of branch nodes related to root node.

So I used

MATCH (b:branch)-[:branch_of]-(r:root {Root:'A'}) RETURN count(b) 

It works for Single root, But I want multiple root- so i used

MATCH (r:root),(b:branch),(b)-[:branch_of]-(r {Root:'A'},{Root:'B'},{Root:'C'}) RETURN count(b) 

But its not working -How to Implement this?

Upvotes: 0

Views: 198

Answers (2)

cybersam
cybersam

Reputation: 66977

I think this is closer to what you want. For every root (that has a 'Root' property value in the 'roots' parameter array), get a count of the number of branches.

By the way, you should indicate the directionality of the relationships in the query. That can make things faster.

MATCH (b:branch)-[:branch_of]-(r:root)
WHERE r.Root IN {roots}
RETURN r, count(b)

So, if you only cared about roots "A" and "B", you'd specify a parameter like this:

{
  "roots": ['A', 'B']
}

Upvotes: 1

Akash Kumar
Akash Kumar

Reputation: 642

Got answer myself

MATCH (r:root),(b:branch),(b)-[:branch_of]-(r) WHERE r.name="A" OR r.name="B" RETURN count(b)

It worked

Upvotes: 0

Related Questions