执行查询后,如何了解特定用户的排名?这是在SQL Server 2005中。例如,我使用的查询是:
CREATE PROCEDURE [dbo].[getmyRankinContest]
@UserID int
AS
BEGIN
select userid,
from
invitecount
group by userid
order by sum(points) desc
END
但@UserID
是UserID
。如何在上面的代码中添加@UserID
来获得我的排名?有什么建议吗?
提前致谢..
答案 0 :(得分:4)
您可以尝试这样的事情:
CREATE PROCEDURE [dbo].[getmyRankinContest] @UserID int
AS
BEGIN
;WITH CTE AS
(
SELECT userId, RANK() OVER(ORDER BY Points DESC) Ranking
FROM ( SELECT userid, SUM(points) Points
FROM invitecount
GROUP BY userid) A
)
SELECT *
FROM CTE
WHERE UserId = @UserID
END