SQL查询以生成排行榜并选择一个玩家信息

时间:2018-10-06 21:55:52

标签: mysql sql

假定我们具有以下MYSQL查询来生成排行榜:

SELECT x.player_id, x.position,x.leaderboard_value

FROM (SELECT player_id,@rownum := @rownum + 1 AS position,leaderboard_value
      FROM table1
      JOIN (SELECT @rownum := 0) r
      WHERE restrictive_value<200
      ORDER BY leaderboard_value DESC) x

产生这样的页首横幅:

leaderboard

文本文件为CSV:https://www.dropbox.com/s/70xw3ocbonqs98s/sql.csv?dl=0

如何更改或扩展查询以提供特定球员排名以及排名总数(最高排名)

对于上表和player_id = 10649,我希望结果表有1行包含以下字段:

位置:6 总排名:20 百分比= 0.3(即6/20)

此操作的背景是将该百分比与定义排名的其他表格rank_map(rank_id,minvalue,maxvalue)结合在一起(例如,当此值介于0.2和0.4之间时,rank为“ 9”)

最后,该查询应该简单地返回 排名:9 作为答案

非常感谢您。

更新:

使用以下查询与Gordon Linoffs回答:

SELECT MAX(CASE WHEN player_id = 10649 THEN position END) as play_position,
   COUNT(*) as total_position,
   MAX(CASE WHEN player_id = 10649 THEN position END) / COUNT(*) as ratio
FROM (SELECT t1.*, @rownum := @rownum + 1 AS position
  FROM table1 t1 CROSS JOIN
       (SELECT @rownum := 0) r
  WHERE restrictive_value < 200
  ORDER BY leaderboard_value DESC
 ) x;

我可以得到这张桌子: second step

还剩下最后一步,即如何将其与rank(rank_id,minvalue,maxvalue)表联接,以仅获得比率在minvalue和maxvalue之间的rank_id行?

2 个答案:

答案 0 :(得分:2)

为此,您可以使用条件聚合:

SELECT MAX(CASE WHEN player_id = 10649 THEN position END) as play_position,
       COUNT(*) as total_position,
       MAX(CASE WHEN player_id = 10649 THEN position END) / COUNT(*) as ratio
FROM (SELECT t1.*, @rownum := @rownum + 1 AS position
      FROM table1 t1 CROSS JOIN
           (SELECT @rownum := 0) r
      WHERE restrictive_value < 200
      ORDER BY leaderboard_value DESC
     ) x;

您可以使用COUNT(*)MAX(position)来获得最高排名。

答案 1 :(得分:2)

您可以通过向子查询COUNT(*)中添加JOIN来获得总行数,然后将百分比显示为排名/总数:

SELECT x.player_id, x.position,x.leaderboard_value, (x.position/x.total) AS percentage
FROM (SELECT total, player_id,@rownum := @rownum + 1 AS position,leaderboard_value
      FROM table1
      JOIN (SELECT @rownum := 0) r
      JOIN (SELECT COUNT(*) AS total FROM table1) c
      WHERE restrictive_value<200
      ORDER BY leaderboard_value DESC) x

一些示例数据的输出:

player_id   position    leaderboard_value   percentage
2730        1           1090                0.01
1369848     2           1017                0.02
1665922     3           960                 0.03
1607632     4           910                 0.04
1853500     5           909                 0.05
10649       6           883                 0.06
1538490     7           877                 0.07
1898051     8           866                 0.08
1510162     9           828                 0.09
1898129     10          825                 0.1
1863538     11          821                 0.11
1522562     12          806                 0.12
1380267     13          805                 0.13
1404318     14          797                 0.14
8793        15          769                 0.15
21793       16          767                 0.16
14658       17          756                 0.17
1690659     18          729                 0.18
1429094     19          723                 0.19
1727977     20          719                 0.2

SQLFiddle Demo

要仅获取特定玩家的数据,只需添加一个WHERE x.player_id=nnnn子句,例如

SELECT x.player_id, x.position,x.leaderboard_value, (x.position/x.total) AS percentage
FROM (SELECT total, player_id,@rownum := @rownum + 1 AS position,leaderboard_value
      FROM table1
      JOIN (SELECT @rownum := 0) r
      JOIN (SELECT COUNT(*) AS total FROM table1) c
      WHERE restrictive_value<200
      ORDER BY leaderboard_value DESC) x
WHERE x.player_id = 10649

输出:

player_id   position    leaderboard_value   percentage
10649       6           883                 0.06

要从排名表中获得排名,您只需要根据百分比JOIN(请注意,您必须使用公式,因为不能在JOIN中使用别名) :

SELECT x.player_id, x.position,x.leaderboard_value, (x.position/x.total) AS percentage, m.rank_id
FROM (SELECT total, player_id,@rownum := @rownum + 1 AS position,leaderboard_value
      FROM table1
      JOIN (SELECT @rownum := 0) r
      JOIN (SELECT COUNT(*) AS total FROM table1) c
      WHERE restrictive_value<200
      ORDER BY leaderboard_value DESC) x
JOIN rank_map m ON m.minvalue <= (x.position/x.total) AND m.maxvalue > (x.position/x.total)
WHERE x.player_id = 10649

输出:

player_id   position    leaderboard_value   percentage  rank_id
10649       6           883                 0.06        3

Updated Demo