Cypher query for return node which <some_field> = max(<some_field>) value out of a group of nodes

Suppose I have the following data:

field1, field2
1,1
1,4
1,3
2,5
2,2
3,1
3,8
3,9
3,11
4,4

How can I write a cypher query to return the node that has the biggest value of field2 for a given field1 . i.e.

1,4
2,5
3,11
4,4

My attempts so far have resulted in the following (which doesn't work):

MATCH(a:A)
WITH COLLECT({field1: a.field1, field2: MAX(a.field2)}) as rowInfos
UNWIND rowInfos as rowInfo
WITH rowInfo.field1 as field1, rowInfo.field2 as maxField2
MATCH (a)
WHERE a.field1 = field1 AND a.field2 = maxField2
return a

If these nodes were rows in a SQL database then the following query would be used:

SELECT
a.* from A
INNER JOIN (
SELECT
field1, max(field2) as field2
FROM A
GROUP BY
field1
) ut
ON
a.field1 = ut.field1
AND a.field2 = ut.field2

Here is a solution,

MATCH (f:Fld1)-->(g:Fld2)
RETURN f.f1 as Fld1, max(g.f2) as Fld2;

Result:
lsmm2

MATCH (f:Fld1)-->(g:Fld2)
WITH f.f1 as Fld1, max(g.f2) as Fld2
MATCH (f1:Fld1)-->(g1:Fld2)
WHERE g1.f2 IN [Fld2]
RETURN f1, g1;

Result:
lsmm1

Turns out the query to achieve this is rather simple:

MATCH(b:A)
WITH b.field1 as field1, MAX(b.field2) as field2
WHERE b.field1 = field1
MATCH (b)
WHERE b.field1 = field1 AND b.field2 = field2
RETURN b

You don't need that first WHERE clause, the aggregation you're doing on line 2 is enough to get your groupings correct.

I've got a new aggregating APOC proc committed which should be able to more concisely handle a case like this, giving you all items that have the max (or min) of a certain value without having to do a rematch as in this query. That should go out with the next APOC release.