我知道之前也曾问过这个问题,但我尝试了所有问题,但没有人为我工作,因为我的查询有些不同,因为它在查询中具有求和功能,需要求和和得到用户的排名。
所以我的问题是如何为我的表格查找单个用户的排名:
当前,我正在尝试使用此查询,但这给了我所有用户。
SELECT u.user_name as userName
, sum(taken_quiz_points) as totalPoints
FROM taken_quiz as q
, users_app as u
WHERE q.taken_quiz_user_id = u.user_id
GROUP
BY taken_quiz_user_id
ORDER
BY totalPoints DESC
答案 0 :(得分:0)
首先,当您从多个表中查询数据时,应使用INNER JOIN。
另外,我认为不可能对别名进行ORDER,您必须重复别名的操作。
如果要具有单个用户的等级,则必须添加WHERE条件,例如WHERE u.name = "toto"
之类的东西。
您应该以这个结尾:
SELECT
u.user_name as userName ,
sum(q.taken_quiz_points) as totalPoints
FROM taken_quiz q
INNER JOIN users_app u ON q.taken_quiz_user_id = u.user_id
WHERE u.name = "toto"
GROUP BY q.taken_quiz_user_id
ORDER BY sum(q.taken_quiz_points) DESC
答案 1 :(得分:0)
因此,您需要整个数据集才能知道该人的排名。这有点丑陋,但是您可以嵌套以获取排名,然后从排名数据集中选择您感兴趣的人:-
Select userName, user_rank from
(Select userName,totalpoints ,dense_rank() OVER (Order by totalpoints desc) as user_rank
from (
SELECT u.user_name as userName ,sum(taken_quiz_points) as totalPoints
FROM taken_quiz as q, users_app as u
WHERE q.taken_quiz_user_id=u.user_id GROUP BY user_name,
taken_quiz_user_id) aa
) bb
where userName ='bob'