Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3287892
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:32:41+00:00 2026-05-17T20:32:41+00:00

I have looked at multiple other question similarly asked on StackOverflow, but nothing seems

  • 0

I have looked at multiple other question similarly asked on StackOverflow, but nothing seems to fit my bill. My query is slightly more complex. Essentially, I need to find the rank of the entry.

My table structure is:

TestEntry

Id, TotalTime, DateCreated

GameResult

GameId, TestEntryId, Duration, Score

QuestionResult

QuestionId, TestEntryId, Correct, UsersAnswer

The query to calculate all the scores is done via the following:

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

Based on a specific TestEntry.Id, I need to determine the rank of that entry. Proving tricky because of the usage of temp tables, and the fact there is not a “TotalScore” in the TestEntry table, it is being dynamically calculated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:32:42+00:00Added an answer on May 17, 2026 at 8:32 pm

    Unless theres a pressing need for temp tables, sack them and use common table expressions instead. Then use the RANK function to get a ranking for each id.

    ;WITH GRS AS
    (
        SELECT 
            TestEntryId, 
            SUM(Score) AS Score 
        FROM GameResult
        GROUP BY TestEntryId
    ),
    QRS AS
    (
    SELECT 
            TestEntryId, 
            COUNT(*) CorrectAnswers
        FROM QuestionResult
        WHERE Correct = 1
        GROUP BY TestEntryId
    ),
    Scores AS
    (
    SELECT 
            Id, ISNULL(GRS.Score,0) + (ISNULL(QRS.CorrectAnswers,0) * 25) AS Score
        FROM TestEntry TE
        LEFT JOIN GRS ON(GRS.TestEntryId = TE.Id)
        LEFT JOIN QRS ON(QRS.TestEntryId = TE.Id)
        WHERE TE.TotalTime > 0
    )
    SELECT Id,Score,RANK() OVER (ORDER BY Score DESC) AS [Rank] FROM Scores
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've looked at a bunch of resources provided by similar questions asked on this
I have an application that allows the user to edit multiple text fields and
I have looked around for the past year to put tide predictions on a
I have a distributed application that uses .NET Remoting on an in-house gigabit network.
I have an ASP.NET MVC application that also employs the typical NHibernate / Castle
I have information on school athletics, with tables for school, season, cashflow, and cashflow_group.
I'm developing a series of applications for mobile devices, we'll call them Orange, Cherry,
I'm setting up my freelance server which will be using Mercurial for all the
I am trying to get JsTree to work to get a folder view in
I've been using Perforce for a number of years. I'd like to switch to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.