Jordan.J.D
Jordan.J.D

Reputation: 8113

Neo4j count and order relationships to list of nodes

I have a graph of people and items. The relationship is one to many (a:Person)-[:has]->(b:Item). For an input list of items, I am trying to get a list of people that have that item, ordered by who has the most items in the list.

enter image description here

In the above image I would expect an output like:

[{"Person 1", count: 3},{"Person 2", count: 2},{"Person 3", count: 1}]

I tried a few variations of:

match(a:Person) match(b:Item) where b.label in ["Item 1", "Item 2", "Item 3"] return a,count( (a)-[:has]->(b))

and

match(n:Item) return n, size((n)-->()) as count where the counts are correct but when I try to narrow the Items to a list the count is wrong.

Upvotes: 0

Views: 226

Answers (1)

Lju
Lju

Reputation: 590

Perhaps something like:

MATCH (a:Person)-[:has]->(b:Item)
RETURN a, count(b) ORDER by count(b) DESC

Upvotes: 2

Related Questions