I have this project that it ranks different items by their scores, the ranking is okay but it shows gaps when there is a tied score.
Here is the query:
S
You basically need Dense_Rank() like functionality (available in MySQL version >= 8.0). In older versions of MySQL, it can be emulated using Session Variables.
scc_bgyscoretotal
(highest value having rank 1 and so on). Firstly, get unique values of scc_bgyscoretotal
, and then determine their ranking using Session Variables.bgyprofile
using scc_bgyscoretotal
. Try the following:
SELECT t2.bgycode,
t2.scc_bgyscoretotal,
dt2.`rank`
FROM bgyprofile AS t2
JOIN
(
SELECT dt1.scc_bgyscoretotal,
@rank_no := @rank_no + 1 AS `rank`
FROM
(
SELECT t1.scc_bgyscoretotal
FROM bgyprofile AS t1
GROUP BY t1.scc_bgyscoretotal
ORDER BY t1.scc_bgyscoretotal DESC
) AS dt1
CROSS JOIN (SELECT @rank_no := 0) AS init1
) AS dt2 ON dt2.scc_bgyscoretotal = t2.scc_bgyscoretotal