Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
04-12-2020 02:55 PM
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...
04-12-2020 11:58 PM
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
04-14-2020 07:24 AM
Probably you mean (a:A)-[x:X]->(b:B)<-[y:Y]-(c:C)
04-14-2020 07:27 AM
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
04-14-2020 08:05 AM
You can try
MATCH (a:A)-[ x ]->(b:B)<--(c:C)
WHERE count(c) > 10
RETURN a.key, count(distinct b)
04-22-2020 06:52 PM
Unfortunately that's not possible, aggregations must occur in WITH or RETURN clauses, not in WHERE clauses.
04-14-2020 02:57 PM
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.
04-22-2020 06:55 PM
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.
All the sessions of the conference are now available online