I need to find a minimum value from limited number of rows in single column. Let's say I have this results
table:
-----------------------------------------
| id | category | class | score |
-----------------------------------------
| 1 | contest1 | seven | 55 |
| 2 | contest1 | sixth | 78 |
| 3 | contest2 | seven | 20 |
| 4 | contest1 | eleven | 21 |
| 5 | contest2 | eleven | 56 |
| 6 | contest3 | ten | 66 |
| 7 | contest3 | ten | 90 |
| 8 | contest3 | nine | 91 |
| 9 | contest2 | seven | 30 |
| 10 | contest1 | nine | 51 |
-----------------------------------------
Now I need to create a set of values which are >= 50 AND limited to 3 rows. Like this:
-----------------------------------------
| id | category | class | score |
-----------------------------------------
| 1 | contest1 | seven | 55 |
| 2 | contest1 | sixth | 78 |
| 10 | contest1 | nine | 51 |
-----------------------------------------
In this set I have to find MIN value, which is quite simple actually, but only if I do it manually for each category. This query works perfectly:
SELECT MIN(t1.score)
FROM (
SELECT category, score
FROM results
WHERE category = "contest1" AND score >= 50
ORDER BY score DESC
LIMIT 3
) t1
And gives the right result:
-------------------
| MIN(t1.score) |
-------------------
| 51 |
-------------------
However, I can't write a query which will do it automatically for each category. In a real table there are over a hundred categories with thousands of score values. How can I find minimum value for each category within a limited set of rows?