The aggregate function min returns the minimum value from the set

MATCH (n:Person) RETURN min(n.property) 

Imagine a condition that a person has from one to several cars. We need to get people with cars that have the lowest cost:

 MATCH (p:Person)-[r:has_many]->(c:Cars) RETURN p, min(с.price) 

So we get almost what we need, but we still have to get the car itself. Here is a snag.

 MATCH (p:Person)-[r:has_many]->(c:Cars) RETURN p, min(с.price), c 

It does not work. And even more fun if several cars from the owner with the same value. I tried different options .. while stalled on this

 MATCH (p:Person)-[r:has_many]->(c:Cars) WITH p, min(c.price) as minPrice MATCH (p:Person)-[r:has_many]->(c:Cars) WHERE c.price = minPrice RETURN p, c 

But it is not yet invented how to group the same Person as a result of the output.

    1 answer 1

    in order to group the same faces you need to add the collect () aggregate to the machines:

     MATCH (p:Person)-[r:has_many]->(c:Cars) WITH p, min(c.price) as minPrice MATCH (p:Person)-[r:has_many]->(c:Cars) WHERE c.price = minPrice RETURN p, collect(c) as cars