cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Getting a node that has a relationship with another node that is connected to at least X nodes

Hello.

I just started with Neo4j and I can't find an answer to my question or tried to solve it the wrong way because none of it was at least close to solving my problem.

I have graph where part of it looks like this
(A)-[ x ]->(B)<-[ y ]-(C)
I need to get the A node that has connected B nodes with at least 10 C nodes connected to it and get the B node amount, so it will give a result like this.

A.key    |   B node amount
-----------------------------------
1           |   7
2           |   4
6           |   2

It's probably really simple but I have some sort of "mind blockade" and I can't get through it...

7 REPLIES 7

intouch_vivek
Graph Steward

Hi @knoxrnox,

Welcome to the community !!!
could you please show us the graph pic.

However you can try like
match (a:A)-[x:X]->(b:B)-[y:Y]->(c:C) with a , b , count(y) as y where y>=10 return a ,b,y

Probably you mean (a:A)-[x:X]->(b:B)<-[y:Y]-(c:C)

oh Yes. i forgot to change that..
I made sample dataset and relationship direction as above however @knoxrnox can try with the his own dataset and relationship direction

anthapu
Graph Fellow

You can try

MATCH (a:A)-[ x ]->(b:B)<--(c:C)
WHERE count(c) > 10
RETURN a.key, count(distinct b)

Unfortunately that's not possible, aggregations must occur in WITH or RETURN clauses, not in WHERE clauses.

Than you all. That's exactly what I was trying to do and now when I see the answer I feel really stupid...
Thank you once again.

Alternately you can use the size() function in a WHERE clause to get the count of paths matching the pattern:

MATCH (a:A)-[x:X]->(b:B)
WHERE size((b)<-[:Y]-(:C)) > 10
RETURN a.key as a, count(b) as bCount

Though note that we do not know that b is connected to more than 10 distinct :C nodes...if multiple :Y relationships from a :B node can connect to the same :C node, then this won't be accurate. If you need counts to distinct :C nodes, then you will have to MATCH out the whole pattern and do a series of count aggregations.