Lars Stenberg
Lars Stenberg

Reputation: 241

Order by relationship count

I have a graph which looks like this:

User->Friend->Area 
|             (Which area does this friend live in)
------------->Area<-Friend<-Other users
              (Which area does this user live in)

If i like to find all areas where i dont have friends, i do this

start user=node(reference)
match user-->friend-->area<-[r?:HAVE_A]-friend<--user
where r is null
return area

Works great!

But how to order the results so i get the area which have the most users first?

Upvotes: 1

Views: 207

Answers (1)

Edward
Edward

Reputation: 3276

If I'm understanding your question correctly, you are getting the correct areas returned but want to organize them by the count of users.

In that case you need a WITH clause to get the users, COUNT them, and an ORDER BY to sort them:

START user=node(reference)
MATCH user-->friend-->area<-[r?:HAVE_A]-friend<--user
WHERE r is null
WITH area 
MATCH area<--users
RETURN area, COUNT(users) as cnt
ORDER BY cnt DESC

Upvotes: 2

Related Questions