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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:15:20+00:00 2026-05-13T12:15:20+00:00

So I know this problem isn’t a new one, but I’m trying to wrap

  • 0

So I know this problem isn’t a new one, but I’m trying to wrap my head around it and understand the best way to deal with scenarios like this.

Say I have a hypothetical table ‘X’ that looks like this:

GroupID ID (identity)   SomeDateTime
--------------------------------------------
1       1000        1/1/01
1       1001        2/2/02
1       1002        3/3/03
2       1003        4/4/04
2       1004        5/5/05

I want to query it so the result set looks like this:

----------------------------------------
1       1002        3/3/03
2       1004        5/5/05

Basically what I want is the MAX SomeDateTime value grouped by my GroupID column. The kicker is that I DON’T want to group by the ID column,
I just want to know the ‘ID’ that corresponds to the MAX SomeDateTime.

I know one pseudo-solution would be:

;WITH X1 as (
    SELECT MAX(SomeDateTime) as SomeDateTime, GroupID 
    FROM X
    GROUP BY GroupID
)
SELECT X1.SomeDateTime, X1.GroupID, X2.ID
FROM X1
    INNER JOIN X as X2
        ON X.DateTime = X2.DateTime

But this doesn’t solve the fact that a DateTime might not be unique. And it seems sloppy to join on a DateTime like that.

Another pseudo-solution could be:

SELECT X.GroupID, MAX(X.ID) as ID, MAX(X.SomeDateTime) as SomeDateTime
FROM X
GROUP BY X.GroupID

But there are no guarantees that ID will actually match the row that SomeDateTime comes from.

A third less useful option might be:

SELECT TOP 1 X.GroupID, X.ID, X.SomeDateTime
FROM X
WHERE X.GroupID = 1
ORDER BY X.SomeDateTime DESC

But obviously that only works with a single, known, GroupID. I want to be able to join this result set on GroupID and/or ID.

Does anyone know of any clever solutions? Any good uses of windowing functions?

Thanks!

  • 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-13T12:15:20+00:00Added an answer on May 13, 2026 at 12:15 pm

    I think this will do what you want.

    ;WITH X1 
    AS 
    (
        SELECT SomeDateTime
               ,GroupID 
               ,ID
               ,ROW_NUMBER() OVER (PARTITION BY GroupID
                                   ORDER BY SomeDateTime DESC
                                   ) AS rn
        FROM X
    )
    SELECT SomeDateTime
           ,GroupID
           ,ID
    FROM X1
    WHERE rn = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Yes I know, this title isn't really helpfull but this is the exact problem.
I know this isn't a direct technical problem but this seems like an ideal
I know this subjet isn't new at all but i'm going to be crazy
I know I've solved this problem before, but I can't remember or find the
I don't know if anyone else even has this problem, or has noticed, but
I know this isn't the right place to ask about this specific vague problem,
I don't know if it is a common problem, but this strange problem is
I know this seems to be a common one, but I'm lost with it.
I do not know how common this problem is for other users, but for
I know this question isn't directly programming related, but since I want to be

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.