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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:47:31+00:00 2026-05-31T09:47:31+00:00

was hoping some-one could help me with this: My table is: id Version datetime

  • 0

was hoping some-one could help me with this:
My table is:

id  Version  datetime     name     resource 
---|--------|------------|--------|--------- 
1  | 1      | 03/03/2009 | con1   | 399  
2  | 2      | 03/03/2009 | con1   | 244 
3  | 3      | 01/03/2009 | con1   | 555 
4  | 1      | 03/03/2009 | con2   | 200 
5  | 2      | 03/03/2009 | con2   | 500 
6  | 3      | 04/03/2009 | con2   | 600 
7  | 4      | 31/03/2009 | con2   | 700 

I need to select each distinct “name” that has greatest value of “datetime” that less than or equal to a given date; and where the version is the maximum version if there are multiple records that satisfy the first condition.

The result if the given date were ’04/03/2009′ would be:

id  Version  datetime     name     resource 
---|--------|------------|--------|--------- 
2  | 2      | 03/03/2009 | con1   | 244  
6  | 3      | 04/03/2009 | con2   | 600 

Currently I’ve created the following query, which works, but I suspect it’s not the best when it comes to performance when run on a large table:

SELECT [id], [Version], [datetime], [name], [resource]  
FROM theTable
WHERE [Version] = 
(
SELECT MAX(Version) FROM theTable AS theTable2 WHERE theTable.[name] = theTable2.[name] 
AND  theTable2.[datetime] = 
    (
    SELECT MAX(theTable3.[datetime]) FROM theTable AS theTable3 
    WHERE theTable2.[name] = theTable3.[name] AND theTable3.[datetime] <= '04/03/2009'
    )
)

I’d appreciate if some-one could suggest a more efficient way to do this; and if possible, provide an example:-).

Thanks in advance.

  • 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-31T09:47:32+00:00Added an answer on May 31, 2026 at 9:47 am

    You can use PARTITION BY. This lets you basically rank the results. In your instance, you then want to select only the result with ranking 1. First, filter out the results with invalid date times (using WHERE), then in the partition, order by the columns descending (thus, the first result would be the one with the maximum datetime, and, in case of datetime tie, the maximum version as well.)

    SELECT [id], [Version], [datetime], [name], [resource]
    FROM
    (
       SELECT [id], [Version], [datetime], [name], [resource], row_number() 
         OVER (PARTITION BY [name] ORDER BY [datetime] DESC, [Version] DESC) as groupIndex
       FROM theTable
       WHERE [datetime] <= '04/03/2009'
    ) AS t
    WHERE groupIndex = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm hoping to get some help with this weird problem. We're running the Jacorb
I was hoping someone could help me out with this stupid problem I'm having
first time poster. Was hoping someone could help me with this. In section 5.10,
I was hoping someone could provide me with some basic code help in Scala.
I'm having trouble with one particular issue, I was hoping someone could help me
I am experiencing an unexpected behaviour and was hoping someone could help with some
Hoping someone can help me out with this. Lets say I have a table
I was hoping someone could help me in resolving this issue. I have 3
I was hoping someone could help me with a little issue I'm having trouble
Im using the .NET framework 1.1 and Im hoping someone could help me implement

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.