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 3781288
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:55:27+00:00 2026-05-19T10:55:27+00:00

Given a table of responses with columns: Username, LessonNumber, QuestionNumber, Response, Score, Timestamp How

  • 0

Given a table of responses with columns:

Username, LessonNumber, QuestionNumber, Response, Score, Timestamp

How would I run a query that returns which users got a score of 90 or better on their first attempt at every question in their last 5 lessons? “last 5 lessons” is a limiting condition, rather than a requirement, so if they completely only 1 lesson, but got all of their first attempts for each question right, then they should be included in the results. We just don’t want to look back farther than 5 lessons.

About the data: Users may be on different lessons. Some users may have not yet completed five lessons (may only be on lesson 3 for example). Each lesson has a different number of questions. Users have different lesson paths, so they may skip some lesson numbers or even complete lessons out of sequence.

Since this seems to be a problem of transforming temporally non-uniform/discontinuous values into uniform/contiguous values per-user, I think I can solve the bulk of the problem with a couple ranking function calls. The conditional specification of scoring above 90 for “first attempt at every question in their last 5 lessons” is also tricky, because the number of questions completed is variable per-user.

So far…

As a starting point or hint at what may need to happen, I’ve transformed Timestamp into an “AttemptNumber” for each question, by using “row_number() over (partition by Username,LessonNumber,QuestionNumber order by Timestamp) as AttemptNumber”.

I’m also trying to transform LessonNumber from an absolute value into a contiguous ranked value for individual users. I could use “dense_rank() over (partition by Username order by LessonNumber desc) as LessonRank”, but that assumes the order lessons are completed corresponds with the order of LessonNumber, which is unfortunately not always the case. However, let’s assume that this is the case, since I do have a way of producing such a number through a couple of joins, so I can use the dense_rank transform described to select the “last 5 completed lessons” (i.e. LessonRank <= 5).

For the >90 condition, I think I can transform the score into an integer so that it’s “1” if >= 90, and “0” if < 90. I can then introduce a clause like “group by Username having SUM(Score)=COUNT(Score).”, which will select only those users with all scores equal to 1.

Any solutions or suggestions would be appreciated.

  • 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-19T10:55:28+00:00Added an answer on May 19, 2026 at 10:55 am

    You kind of gave away the solution:

    SELECT DISTINCT Username
    FROM Results 
    WHERE Username NOT in (
        SELECT DISTINCT Username
        FROM (
            SELECT
                r.Username,r.LessonNumber, r.QuestionNumber, r.Score, r.Timestamp
                , row_number() over (partition by r.Username,r.LessonNumber,r.QuestionNumber order by r.Timestamp) as AttemptNumber
                , dense_rank() over (partition by r.Username order by r.LessonNumber desc) AS LessonRank
            FROM Results r
            ) as f
        WHERE LessonRank <= 5 and AttemptNumber = 1 and Score < 90
    )
    

    Concerning the LessonRank, I used exactly what you desribed since it is not clear how to order the lessons otherwise: The timestamp of the first attempt of the first question of a lesson? Or the timestamp of the first attempt of any question of a lesson? Or simply the first(or the most recent?) timestamp of any result of any question of a lesson?

    The innermost Select adds all the AttemptNumber and LessonRank as provided by you.

    The next Select retains only the results which would disqualify a user to be in the final list – all first attempts with an insufficient score in the last 5 lessons. We end up with a list of users we do not want to display in the final result.

    Therefore, in the outermost Select, we can select all the users which are not in the exclusion list. Basically all the other users which have answered any question.

    EDIT: As so often, second try should be better…

    One more EDIT:

    Here’s a version including your remarks in the comments.

    SELECT Username
    FROM 
    (
        SELECT Username, CASE WHEN Score >= 90 THEN 1 ELSE 0 END AS QuestionScoredWell
        FROM (
            SELECT
                r.Username,r.LessonNumber, r.QuestionNumber, r.Score, r.Timestamp
                , row_number() over (partition by r.Username,r.LessonNumber,r.QuestionNumber order by r.Timestamp) as AttemptNumber
                , dense_rank() over (partition by r.Username order by r.LessonNumber desc) AS LessonRank
            FROM Results r
            ) as f
        WHERE LessonRank <= 5 and AttemptNumber = 1
    ) as ff
    Group BY Username
    HAVING MIN(QuestionScoredWell) = 1
    

    I used a Having clause with a MIN expression on the calculated QuestionScoredWell value.

    When comparing the execution plans for both queries, this query is actually faster. Not sure though whether this is partially due to the low number of data rows in my table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a response that sends back an entire table. N number of rows
So I have a table with two columns; treatments in the first and responses
Given the following tables: Topic id, last_updated_child_id Response id, topic_id, updated_at How do I
Given table: ID ONE TWO X1 15 15 X2 10 - X3 - 20
Given a table where the first column is seconds past a certain reference point
Given a table of logical resource identifiers (one per row), what is the best
Given a table row, I want to get the HTML out of the span
Given a table containing dotted quad IPv4 addresses stored as a VARCHAR(15) , for
Given a table (id, col1, col2), does it make sense to create the following
Given a table with employee statuses and effective dates, how can I retrieve just

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.