If I have a table Numbers with data:
+------+------+------+------+
| colA | colB | colC | colD |
+------+------+------+------+
| 1 | 2 | 3 | 4 |
| 1 | 2 | 9 | 5 |
+------+------+------+------+
and do:
select colA, colB, colC, MAX(colD) FROM Numbers GROUP BY colA, colB;
I believe that it should return row 2. It groups by colA, colB and picks the largest one in colD.
Unfortunately this doesn’t work, because you also have to group by colC in order to return it.
Why? Is there another way to do what I am trying to do?
I want the row with same in colA and colB but the largest one in colD.
There are a couple of ways to handle this. Perhaps the easiest is a
JOINagainst a subquery which does thecolA, colBgroup, and finds the full corresponding row from that.