get the mode of an aggregation

Viewed 25

I am learning about neo4j and I was asked to find the most common category of business related to a user by reviews. Since category doesn't actually exist in the data, and in fact there's just one business, I used name instead. But the way I did this doesn't give me exactly what I want:

MATCH (u:User {name: "Willie"})-[:WROTE]-(:Review)-[:REVIEWS]-(b:Business) 
  RETURN b.name, count(b.name)
  ORDER BY count(b.name)
  LIMIT 1

things that don't seem right:

  • am I being efficient using count(b.name) twice? (is there a way to refer to a variable? I want to be sure that this is not reinitiating a count.
  • I am getting a name and a count, but I just want a name.
  • there might be a better aggregation for this? Is there any way to instead use something like:
MATCH (u:User {name: "Willie"})-[:WROTE]-(:Review)-[:REVIEWS]-(b:Business)
  RETURN mode(b.name)
1 Answers

You can use a variable for count(b.name) and return only the name by using the "WITH" clause.

MATCH (u:User {name: "Willie"})-[:WROTE]-(:Review)-[:REVIEWS]-(b:Business) 
  WITH b.name as name, count(b.name) as cnt ORDER BY cnt desc
  RETURN name LIMIT 1
Related