I have a log table which has host_name, drive_id and process_run_id as primary key.
I need the MySQL statement which will get the top n process_run_ids for each unique host_name and drive_id.
CURRENT TABLE:
|host_name|drive_id |process_run_id|free_space|
|       A |   C     |          1   |       500|
|       A |   C     |          2   |       500|
|       A |   C     |          3   |       570|
|       A |   C     |          4   |      1000|
|       B |   C     |          1   |       769|
|       B |   C     |          2   |      4167|
|       B |   C     |          3   |      3244|
|       B |   D     |          1   |      7654|
|       B |   D     |          2   |        76|
|       B |   D     |          3   |       435|
|       B |   D     |          4   |       243|
|       C |   C     |          1   |     23443|
|       C |   C     |          2   |      4324|
|       C |   C     |          3   |      1232|
|       C |   C     |          4   |      9777|
DESIRED QUERY RESULT (Top 2):
|host_name|drive_id |process_run_id|free_space|
|       A |   C     |          3   |       570|
|       A |   C     |          4   |      1000|
|       B |   C     |          2   |      4167|
|       B |   C     |          3   |      3244|
|       B |   D     |          3   |       435|
|       B |   D     |          4   |       243|
|       C |   C     |          3   |      1232|
|       C |   C     |          4   |      9777|
ATTEMPT :
SELECT space_free, host_name, drive_id, process_run_id
FROM
(SELECT space_free, host_name, drive_id, process_run_id,
              @host_rank := IF(@current_host = CONCAT(host_name, drive_id), @host_rank + 1, 1) AS host_rank,
              @current_host := CONCAT(host_name, drive_id)
   FROM sandbox_yohal.main_fds_history_list
   ORDER BY host_name, process_run_id DESC
 ) ranked
WHERE host_rank <= 2;
