Reputation: 41
I have set of nodes: (u:User)-[:OWN]->(p:Product)
I need to identify the user(s) who owns the most products.
I can get close by using:
MATCH (u:User)-[r]->(p:Product)
RETURN u.value as user, count(p.value) as Pcount
order by Pcount desc
limit 3
user cntProduct
Sammy 16
Bob 16
jo 12
But I'd like to return only "Sammy" and "Bob". So limit is not a good option as I don't know how many users will qualify.
Any ideas how to accomplish this?
Upvotes: 0
Views: 1710
Reputation: 16365
Try this:
MATCH (u:User)-[:OWN]->(p:Product)
WITH u, COUNT(p.value) as count // calc de count for every user
WITH MAX(count) as max // save the 'max' value from 'count' (16, in your case)
MATCH (u:User)-[:OWN]->(p:Product)
WITH u, COUNT(p.value) as count, max
WHERE count = max // pass by WITH only when 'count' is equals 16
RETURN u.value as user, count
Tested here with a little data set and worked fine.
Upvotes: 4