我有一个查询,它会在一天内收回多个事件的总分数。让我们说6.该查询的结果是
LeagueID EntryID UserID TotalPoints TotalBonusPoints TotalPointsLastRace TotalBonusPointsLastRace Position PositionLastRace
3262 37 2 46 3 39 0 1 1
3262 46 4 46 3 33 3 2 4
3262 43 5 37 0 35 0 3 2
3262 40 6 33 3 33 3 4 3
3262 36 1 33 0 21 0 5 5
运行它的查询如下:
SELECT [LeagueID],
[EntryID],
[UserID],
[TotalPoints],
[TotalBonusPoints],
[TotalPointsLastRace],
[TotalBonusPointsLastRace],
ROW_NUMBER() OVER( PARTITION BY [LeagueID] ORDER BY [TotalPoints] DESC, [TotalBonusPoints] DESC) AS [Position],
ROW_NUMBER() OVER( PARTITION BY [LeagueID] ORDER BY [TotalPointsLastRace] DESC, [TotalBonusPointsLastRace] DESC) AS [PositionLastRace]
FROM
(
SELECT [League].[LeagueID],
[Entry].[EntryID],
[UserProfile].[UserID] AS [UserID],
---- get total points for a league for current position
[dbo].[GetTotalPointsForLeague]([UserProfile].[UserID], [League].[LeagueID], 0, 0) AS [TotalPoints],
-- get total points for a league for last race
[dbo].[GetTotalPointsForLeague]([UserProfile].[UserID], [League].[LeagueID], 1, 0) AS [TotalPointsLastRace],
-- get total BONUS points for a league for current position
[dbo].[GetTotalPointsForLeague]([UserProfile].[UserID], [League].[LeagueID], 0, 1) AS [TotalBonusPoints],
-- get total BONUS points for a league for last race
[dbo].[GetTotalPointsForLeague]([UserProfile].[UserID], [League].[LeagueID], 1, 1) AS [TotalBonusPointsLastRace],
FROM [Entry]
INNER JOIN [League] ON [League].[LeagueID] = [Entry].[LeagueID]
INNER JOIN [UserProfile] ON [UserProfile].[UserID] = [Entry].[UserID]
WHERE [Entry].[LeagueID] IN
( SELECT DISTINCT [LeagueID]
FROM [Entry]
INNER JOIN [Meeting] ON [Entry].[MeetingID] = [Meeting].[MeetingID]
WHERE [Entry].[UserID] = @UserID
AND DATEDIFF(d ,CAST(@GetDate AS DATE),CAST([Meeting].[DateTime] AS DATE)) > -4 -- leagues newer than 1 days old
)
AND [Entry].[LeagueID] = 3262
) AS [DATA]
ORDER BY [LeagueID], [Position]
我遇到的问题是,您会看到联盟中的前两个参赛作品具有完全相同的总积分和奖励积分。如果参赛者总得分相同但奖励积分不同,奖励积分最多的积分最高。您将看到底部的两个参赛者具有相同的总积分,但更高的奖励积分是正确的顺序。我的问题是Position和PositionLastRace列。职位应该是:
1,1,2,3,3
我使用位置最后一场比赛来计算位置的移动但我无法知道如何通过Row By实现这一点,有没有人看到这个的快速解决方案?任何帮助将不胜感激。
答案 0 :(得分:2)
考虑:
SELECT [LeagueID],
[EntryID],
[UserID],
[TotalPoints],
[TotalBonusPoints],
[TotalPointsLastRace],
[TotalBonusPointsLastRace],
DENSE_RANK OVER( PARTITION BY [LeagueID] ORDER BY [TotalPoints] DESC, [TotalBonusPoints] DESC) AS [Position],
DENSE_RANK OVER( PARTITION BY [LeagueID] ORDER BY [TotalPointsLastRace] DESC, [TotalBonusPointsLastRace] DESC) AS [PositionLastRace]