Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Return Number of Both Incoming and Outgoing Relationships for Every Node

Tags:

neo4j

I wish to count the number of incoming relationships and the number of outgoing relationships for each node (this gives some insight wrt connectivity).

I can get the number of incoming (or outgoing) using a query like:

MATCH outg=(a)-->(b)
RETURN a.name, labels(a) AS Stereotype,count(rels(outg)) AS out
ORDER BY out DESC

This works.

If, however I try adding incoming relationships:

MATCH outg=(a)-->(b), incom=(c)-->(a)
RETURN a.name, labels(a) AS Stereotype,count(rels(outg)) AS out, count(rels(incom)) AS in
ORDER BY out DESC

then it doesn't produce what I'd expect. In this case both the incoming and outgoing counts are the same and much higher than either on its own (so some sort of multiplication going on).

How should it be done and what's wrong with the logic used in the second case?

like image 454
wikitect Avatar asked Dec 02 '22 14:12

wikitect


1 Answers

I voted for Nicole's and Sam's answers.

However I think there is much more simpler :

MATCH (a)
RETURN id(a), labels(a) as stereotype, 
size((a)-->()) as out, size((a)<--()) as in

You can change id(a) with whatever property you want.

like image 177
Christophe Willemsen Avatar answered Dec 09 '22 09:12

Christophe Willemsen