I have the following table
+----+----------+-----+
| id | priority | sub |
+----+----------+-----+
| 1 | 1 | A |
| 2 | 3 | A |
| 3 | 4 | A |
| 4 | 2 | B |
| 5 | 9 | B |
+----+----------+-----+
I’m trying to get the rows with the highest priority for each sub. So this result:
+----+----------+-----+
| id | priority | sub |
+----+----------+-----+
| 3 | 4 | A |
| 5 | 9 | B |
+----+----------+-----+
I tried grouping, but that gives unpredictable results. My guess was a nested query, first starting to find the highest priority like so:
select max(priority),sub from t group by sub
which gives me
+----------+-----+
| priority | sub |
+----------+-----+
| 4 | A |
| 9 | B |
+----------+-----+
But I also need the ID, adding to the query obviously gives me the wrong ID, joining this result with a query on the same table gave me an error (ER_NONUNIQ_TABLE(1066)), which makes sense… Anyone that can push me in the right direction?
The idea behind the subquery is that it separately gets the maximum value of
PRIORITYfor eachSUB. The result of which will then be joined back to the original table.