我有这张桌子:
Username | score | gameid
SimJoo | 14 | 1
SimJoo | 20 | 1
SimJoo | 23 | 2
Master | 32 | 1
Master | 25 | 2
Master | 9 | 2
对于每个玩家,我需要每个游戏玩家的玩家得分最高的总和除以该游戏玩家获得的最高分数。这对于任何数量的不同玩家来说都是得分和游戏。
上面的示例表的结果是:
Username | total score
Master | 2.000
SimJoo | 1.545
为清楚起见:
SELECT Username, SUM((SELECT MAX(score) WHERE Username=? AND gameid=?)/(SELECT MAX(Score) WHERE gameid=?)) AS total_score
GROUP BY Username
ORDER BY total_score DESC
答案 0 :(得分:3)
第一个子查询返回每个游戏的最大用户分数。第二个 - 游戏最大值
select Username, sum(umax/gmax)
from
(select Username, gameid, max(score) umax
from t
group by Username, gameid
) u
left join
(select gameid, max(score) gmax
from t
group by gameid
) g
on u.gameid = g.gameid
group by Username
答案 1 :(得分:0)
根据您的描述,这需要两个聚合和一个连接:
select t.username, sum(t.score / g.maxscore)
from t join
(select gameid, max(score) as maxscore
from t
group by gameid
) g
on t.gameid = g.gameid
group by t.username;