Ranking system gaps in ranking output

前端 未结 1 405
悲哀的现实
悲哀的现实 2021-01-22 18:42

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         


        
1条回答
  •  囚心锁ツ
    2021-01-22 19:13

    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.

    • In a Derived table, determine ranking of a 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.
    • Now, simply join these Derived table to the main table 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 
    

    0 讨论(0)
提交回复
热议问题