我已经看过类似于StackOverflow询问的其他多个问题,但似乎没有任何东西符合我的账单。我的查询稍微复杂一些。基本上,我需要找到条目的等级。根据游戏结果返回rank的SQL Server查询
我的表结构是:
的TestEntry
Id, TotalTime, DateCreated
GameResult
GameId, TestEntryId, Duration, Score
QuestionResult
QuestionId, TestEntryId, Correct, UsersAnswer
查询来计算所有的分数通过以下完成:
CREATE TABLE #GRS
(
TestEntryId uniqueidentifier,
Score int
)
CREATE TABLE #QRS
(
TestEntryId uniqueidentifier,
CorrectAnswers int
)
/* Populate temp tables with Game/Question results */
INSERT INTO #GRS
SELECT
TestEntryId,
SUM(Score) AS Score
FROM GameResult
GROUP BY TestEntryId
INSERT INTO #QRS
SELECT
TestEntryId,
COUNT(*) CorrectAnswers
FROM QuestionResult
WHERE Correct = 1
GROUP BY TestEntryId
SELECT
Id, ISNULL(GRS.Score,0) + (ISNULL(QRS.CorrectAnswers,0) * 25) AS Score
FROM TestEntry TE
LEFT JOIN #GRS GRS ON(GRS.TestEntryId = TE.Id)
LEFT JOIN #QRS QRS ON(QRS.TestEntryId = TE.Id)
WHERE TE.TotalTime > 0
基于特定TestEntry.Id,我需要确定进入的等级。由于临时表的使用,证明了棘手的问题,事实上TestEntry表中没有“TotalScore”,它正在被动态计算。
什么版本的SQL Server? – 2010-10-28 02:17:43
我第二个OMG Ponies的问题,因为SS08中有一个RANK函数。 – 2010-10-28 02:21:24
@Brian Driscoll:SS2005是第一个支持分析功能的人 - 我同意这一点,这就是我所看到的。 – 2010-10-28 02:23:35