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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:23:22+00:00 2026-06-04T08:23:22+00:00

not sure if there’s an elegant way to acheive this: Data ID Ver recID

  • 0

not sure if there’s an elegant way to acheive this:

Data

ID   Ver   recID  (loads more columns of stuff)  
1     1       1  
2     2       1  
3     3       1  
4     1       2  
5     1       3  
6     2       3  

So, we have ID as the Primary Key, the Ver as the version and recID as a record ID (an arbitary base ID to tie all the versions together).

So I’d like to select from the following data, rows 3, 4 and 6. i.e. the highest version for a given record ID.

Is there a way to do this with one SQL query? Or would I need to do a SELECT DISTINCT on the record ID, then a seperate query to get the highest value? Or pull the lot into the application and filter from there?

  • 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-06-04T08:23:24+00:00Added an answer on June 4, 2026 at 8:23 am

    A GROUP BY would be sufficient to get each maximum version for every recID.

    SELECT  Ver = MAX(Ver), recID
    FROM    YourTable
    GROUP BY
            recID
    

    If you also need the corresponding ID, you can wrap this into a subselect

    SELECT  yt.*
    FROM    Yourtable yt
            INNER JOIN (
              SELECT  Ver = MAX(Ver), recID
              FROM    YourTable
              GROUP BY
                      recID
            ) ytm ON ytm.Ver = yt.Ver AND ytm.recID = yt.RecID
    

    or, depending on the SQL Server version you are using, use ROW_NUMBER

    SELECT  *
    FROM    (
              SELECT  ID, Ver, recID
                      , rn = ROW_NUMBER() OVER (PARTITION BY recID ORDER BY Ver DESC)
              FROM    YourTable
            ) yt
    WHERE   yt.rn = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sure there's a clean way to do this, but I'm probably not using
I'm sure there's more than one way to do this, but what's a good
I'm not sure if there is a more efficient way of doing what I'm
I am not sure if there is a way to update edmx file automatically
I am not sure where i've seen this before, but im sure that there
I am sure there are easier ways to do this, but I am not
I'm sure there's a simple answer to this... or not: I have a site
I'm sure there is a correct way to do this, I'm new to objective-c
This is a bit curly, and I am not sure there is an answer.
This works, but I'm not sure why (and I'm fairly sure there's a smarter

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.