如何获取表格中特定值的行号?这只会导致第1行,我知道这是因为我只返回1行,但我该怎么办呢?
我想知道特定用户与整个表相比的“等级”,所以我希望返回数据如下:
Rank UserId
326 c7f3b047-b5ee-4619-a409-8ff7b29179f8
查询:
DECLARE @UserId uniqueidentifier
SET @UserId='c7f3b047-b5ee-4619-a409-8ff7b29179f8'
SELECT
ROW_NUMBER() OVER (ORDER BY Ratio desc) AS 'Rank',
ROW_NUMBER() OVER (ORDER BY RatioMonth desc) AS 'RankMonth',
ROW_NUMBER() OVER (ORDER BY RatioWeek desc) AS 'RankWeek',
UserMessData.UserId,
aspnet_Users.UserName,
Wins, Losses, Ratio,
WinsMonth, LossesMonth, RatioMonth,
WinsWeek, LossesWeek, RatioWeek
FROM
UserMessData
JOIN
aspnet_Users ON aspnet_Users.UserId = UserMessData.UserId
WHERE
UserMessData.UserId IN (@UserId)
ORDER BY
'Rank' ASC
请帮忙!
答案 0 :(得分:5)
您必须在整个列表中找到用户的位置,然后过滤:
DECLARE @UserId uniqueidentifier
SET @UserId='c7f3b047-b5ee-4619-a409-8ff7b29179f8'
select *
from
(
SELECT ROW_NUMBER() OVER (ORDER BY Ratio desc) AS 'Rank',
ROW_NUMBER() OVER (ORDER BY RatioMonth desc) AS 'RankMonth',
ROW_NUMBER() OVER (ORDER BY RatioWeek desc) AS 'RankWeek',
UserMessData.UserId,
aspnet_Users.UserName,
Wins, Losses,
Ratio,
WinsMonth,
LossesMonth,
RatioMonth,
WinsWeek,
LossesWeek,
RatioWeek FROM UserMessData
JOIN aspnet_Users ON aspnet_Users.UserId=UserMessData.UserId
) a
WHERE a.UserId IN (@UserId)