我希望通过特定活动获得跑步者比赛时间的整体排名。
SELECT a.EventName, b.RunEventName, c.RaceTime, RANK() OVER (ORDER BY RaceTime)Rank
FROM dbo.Event AS a, dbo.RunEvent as b, dbo.RegistrationEvent as c, dbo.Registration as d
WHERE b.EventId = a.EventId
AND c.RunEventId = b.RunEventId
AND c.RegistrationId = d.RegistrationId
AND b.RunEventId = '1'
AND NOT RaceTime IS NULL AND NOT RaceTime = 0
我的查询结果如下所示:
EventName RunEventName RaceTime Rank
--------------------------------------------------------------
1 Event 1 Run 1049 1
2 Event 1 Run 1179 2
3 Event 1 Run 1407 3
4 Event 1 Run 1648 4
5 Event 1 Run 1817 5
6 Event 1 Run 1865 6
如果我执行的SQL语句只显示第5行,我的结果是:
EventName RunEventName RaceTime Rank
----------------------------------------------------------------
1 Event1 Run 1817 1
排名的预期输出应为5,但它显示为1。
答案 0 :(得分:2)
将当前查询用作CTE,然后从CTE中选择所需的行。
WITH cte AS ({your current query})
SELECT * FROM cte
WHERE Rank=5