I am having a hard time understanding how to properly use aggregate functions in Cypher.
Let say I have nodes labelled as Animal, with properties size and species.
For each species, I want to get the largest.
So far, I understand I can do it with the following :
MATCH (n:Animal)
WITH n.species as species, max(n.size) as size
RETURN species, size
And I will effectively get the largest sizes with corresponding species.
But how can I get the nodes instead of species ?
I can't return n because of the WITH statement, and I can't inject it into the WITH because it will break species aggregation.
I know this question has already been asked a few times, but the different solutions I came accross were case-specific and used relations
Any advice is very welcome
EDIT: I finally made it work with :
MATCH (n:Animal)
WITH n.species as species, max(n.size) as size, collect(n) as ns
UNWIND ns as n
WITH n
WHERE n.size = size
RETURN n
Is this the Cypher-way to settle things ? Seems a bit verbose and not efficient (all nodes are fetched here) to me, isn't there a more straightforward option ?