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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:34:53+00:00 2026-05-23T05:34:53+00:00

I have a history table that contains a score per group per date (PK

  • 0

I have a history table that contains a score per group per date (PK is group, date). What is the SQL query that can retrieve the scores for all groups for the 2nd most recent date?

ETA: The dates are the same across groups (each score is entered into the history table at the same time for each group).

  • 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-23T05:34:55+00:00Added an answer on May 23, 2026 at 5:34 am
    select *
    from ScoreHistory sc1
    where exists
    (
        select GroupId, max(ScoreDate) RecentScoreDate
        from ScoreHistory sc2
        where not exists
        (
            select GroupId, max(ScoreDate) RecentScoreDate
            from ScoreHistory sc3
            group by GroupId
            having GroupId = sc2.GroupId and max(ScoreDate) = sc2.ScoreDate
        )
        group by GroupId
        having GroupId = sc1.GroupId and max(ScoreDate) = sc1.ScoreDate
    )
    

    Setup:

    create table ScoreHistory(GroupId int, ScoreDate datetime)
    
    insert ScoreHistory
        select 1, '2011-06-14' union all
        select 1, '2011-06-15' union all
        select 1, '2011-06-16' union all
        select 2, '2011-06-15' union all
        select 2, '2011-06-16' union all
        select 2, '2011-06-17' 
    

    The query would looks as simple as below for MS SQL 2005 +

    ;with cte
    as
    (
        select *, row_number() over(partition by GroupId order by ScoreDate desc) RowNumber
        from ScoreHistory
    )
    select *
    from cte
    where RowNumber = 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that contains a history of costs by location. These are
I have a child table that contains the history of statuses for the particular
I have to write a query to database, that has such table: TABLE HISTORY:
I have a History Table in SQL Server that basically tracks an item through
I have a price history table that has these 4 fields Id Date Product
need help/guide for sql select query, I have 2 table stock and stock_history, in
I have a database that contains a history of product sales. For example the
I want to have a database table that keeps data with revision history (like
I have a table in a database that contains a variety of paths to
Okay so what I have is a table that keeps track of history on

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.