我有一个查询,但没有返回正确的结果, 我希望我的查询返回相同user_id的总分数,以便每个user_id只有一条记录,其中包含所有分数的总和。
我的疑问是:
SELECT
DENSE_RANK() OVER (ORDER BY score DESC) AS rank,
user_id,
SUM(score) AS total_score
FROM
account_game
GROUP BY
user_id, score
ORDER BY
rank ASC
查询输出是:
rank user_id total_score
1 2 4837
2 1 600
2 6 600
3 1 30
4 1 20
user_id 1,2,6应该有三条记录
预期结果应为
rank user_id total_score
-------------------------
1 2 4837
2 6 700
3 1 650
请建议
答案 0 :(得分:3)
正如StuartLC评论的那样,你可以从你的score
中移除GROUP BY
,一切都应该没问题:
SELECT DENSE_RANK() OVER (Order by SUM(score) DESC) AS rank,
user_id,
SUM(score) as total_score
FROM
account_game
GROUP BY user_id
ORDER BY rank ASC