Sort by average of intermediate node property


(Tim) #1

Hello,

I am new to neo4j, and just learning Cypher, so this may be a simple question. Or not. I've had trouble searching for this answer, as I'm not quite sure how to describe it.

In my data, there are a set of Customers, each of which can have zero to many Reviews, which are each associated with 1 or more Products. I need to return a list of Customers who have Reviews that are associated with a specified Product, and sort the list based upon an average of a property in the Review (r.rating), for all of that customer's Reviews on that Product. The average needs to be calculated for each Customer-Product combo.

The following code does not work, and I know that it isn't right. Any hints on how to properly do this?

MATCH (c:Customer)-->(r:Review)-->(p:Product {name: 'ProductName1'})
WITH avg(r.rating) as avg_rating
RETURN c.name, avg_rating
ORDER BY avg_rating DESC
LIMIT 5;

Thank you!

Tim


(Tim) #2

I think I may have solved my own question, but if someone could verify, that'd be great!

MATCH (c:Customer)-->(r:Review)-->(p:Product {name: 'ProductName1'})
WITH c, avg(r.rating) as avg_rating
RETURN c.name, avg_rating
ORDER BY avg_rating DESC
LIMIT 5;