Q:

Selecting a minimum sum of columns for a group

I am having difficulty finding a simple solution to this problem.
You can assume UNIQUE KEY (A, B, C, D, E).

I am trying to SELECT all rows from the above table such that for every unique group of C, D, E, I want the A, B such that A + B <= all other A + B combinations for that particular group of C, D, E.
So performing the SELECT on the above data should yield (in the case of a tie, pick the first one):

If I were to GROUP BY C, D, E, the columns A, B are chosen arbitrarily (technically first A, B encountered by database internally).
If this were relational algebra, I'd cross join the relation with itself, select where C, D, E are equal across 'both' relations and where A + B from one copy of the relation is greater than A + B of the other copy and then subtract the result from the original relation.
I figure though, that there's a simpler way of doing this in MySQL. The table may have thousands of groups of C, D, E, each group containing 5 to 10 A, B. I don't believe a CROSS JOIN would be very efficient.

A:

One way of doing this could be to select the minimum for every combination of c, d, e and then join it with a query on the table:
SELECT mytable.*
FROM   mytable t
JOIN   (SELECT   c, d, e, MIN(a + b) AS min_ab
        FROM     mytable
        GROUP BY c, d, e) s ON t.c = s.c AND
                               t.d = s.d AND
                               t.e = s.e AND
                               t.a + t.b = s.min_ab

