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

  • Home
  • SEARCH
  • 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 6193845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:11:31+00:00 2026-05-24T03:11:31+00:00

I have a table which I want to get the latest entry for each

  • 0

I have a table which I want to get the latest entry for each group. Here’s the table:

DocumentStatusLogs Table

ID DocumentID Status DateCreated
2 1 S1 7/29/2011
3 1 S2 7/30/2011
6 1 S1 8/02/2011
1 2 S1 7/28/2011
4 2 S2 7/30/2011
5 2 S3 8/01/2011
6 3 S1 8/02/2011

The table will be grouped by DocumentID and sorted by DateCreated in descending order. For each DocumentID, I want to get the latest status.

My preferred output:

DocumentID Status DateCreated
1 S1 8/02/2011
2 S3 8/01/2011
3 S1 8/02/2011
  • Is there any aggregate function to get only the top from each group? See pseudo-code GetOnlyTheTop below:

      SELECT
        DocumentID,
        GetOnlyTheTop(Status),
        GetOnlyTheTop(DateCreated)
      FROM DocumentStatusLogs
      GROUP BY DocumentID
      ORDER BY DateCreated DESC
    
  • If such function doesn’t exist, is there any way I can achieve the output I want?

  • Or at the first place, could this be caused by unnormalized database? I’m thinking, since what I’m looking for is just one row, should that status also be located in the parent table?

Please see the parent table for more information:

Current Documents Table

DocumentID Title Content DateCreated
1 TitleA … …
2 TitleB … …
3 TitleC … …

Should the parent table be like this so that I can easily access its status?

DocumentID Title Content DateCreated CurrentStatus
1 TitleA … … s1
2 TitleB … … s3
3 TitleC … … s1

UPDATE
I just learned how to use "apply" which makes it easier to address such problems.

  • 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-24T03:11:32+00:00Added an answer on May 24, 2026 at 3:11 am
    WITH cte AS
    (
       SELECT *,
             ROW_NUMBER() OVER (PARTITION BY DocumentID ORDER BY DateCreated DESC) AS rn
       FROM DocumentStatusLogs
    )
    SELECT *
    FROM cte
    WHERE rn = 1
    

    If you expect 2 entries per day, then this will arbitrarily pick one. To get both entries for a day, use DENSE_RANK instead of ROW_NUMBER.

    As for normalised or not, it depends if you want to:

    • maintain status in 2 places
    • preserve status history
    • …

    As it stands, you preserve status history. If you want latest status in the parent table too (which is denormalisation) you’d need a trigger to maintain "status" in the parent. or drop this status history table.

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

Sidebar

Related Questions

Suppose I have table Person table Employee, which inherits Person. I want to get
I have a table called restaurants which contains each restaurant information, I want to
Currently I have a table variable which I want to get its table content
I have a table for which I want to select top the 5 rows
I have a table on which I want to perform some operations every hour.
I am dynamically creating a table which I want to have clickable rows. When
I have a table in my markup on which I want to add some
i have a table of resources (lets say cars) which i want to claim
I have a table on near the bottom of the page, which I want
I have a repeater control outputting some HTML. I want a table which outputs

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.