如何在执行查询后获取记录的序列号?

时间:2011-07-11 19:44:10

标签: sql sql-server-2005

执行查询后,如何了解特定用户的排名?这是在SQL Server 2005中。例如,我使用的查询是:

CREATE PROCEDURE [dbo].[getmyRankinContest]
@UserID int 
AS
BEGIN

   select userid,
   from
   invitecount
   group by userid
   order by sum(points) desc
END

@UserIDUserID。如何在上面的代码中添加@UserID来获得我的排名?有什么建议吗?

提前致谢..

1 个答案:

答案 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