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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:13:54+00:00 2026-05-27T23:13:54+00:00

Possible Duplicate: SQL: Find the max record per group I have a table with

  • 0

Possible Duplicate:
SQL: Find the max record per group

I have a table with four columns as such:

name   major    minor  revision
p1     0        4      3
p1     1        0      0
p1     1        1      4
p2     1        1      1
p2     2        5      0
p3     3        4      4

This is basically ca table containing records for each version of a program. I want to do a select to get all of the programs and their latest version so the results would look like this:

name   major    minor  revision
p1     1        1      4
p2     2        5      0
p3     3        4      4

I can’t just group by the name and get the max of each column because then i would just end up with the highest number from each column, but not the specific row with the highest version. How can I set this up?

  • 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-27T23:13:55+00:00Added an answer on May 27, 2026 at 11:13 pm

    The way I try to solve SQL problems is to take things step by step.

    • You want the maximum revision for the maximum minor version corresponding to the maximum major version for each product.

    The maximum major number for each product is given by:

    SELECT Name, MAX(major) AS Major FROM CA GROUP BY Name;
    

    The maximum minor number corresponding to the maximum major number for each product is therefore given by:

    SELECT CA.Name, CA.Major, MAX(CA.Minor) AS Minor
      FROM CA
      JOIN (SELECT Name, MAX(Major) AS Major
              FROM CA
             GROUP BY Name
           ) AS CB
        ON CA.Name = CB.Name AND CA.Major = CB.Major
     GROUP BY CA.Name, CA.Major;
    

    And the maximum revision (for the maximum minor version number corresponding to the maximum major number for each product), therefore, is given by:

    SELECT CA.Name, CA.Major, CA.Minor, MAX(CA.Revision) AS Revision
      FROM CA
      JOIN (SELECT CA.Name, CA.Major, MAX(CA.Minor) AS Minor
              FROM CA
              JOIN (SELECT Name, MAX(Major) AS Major
                      FROM CA
                     GROUP BY Name
                   ) AS CB
                ON CA.Name = CB.Name AND CA.Major = CB.Major
             GROUP BY CA.Name, CA.Major
           ) AS CC
        ON CA.Name = CC.Name AND CA.Major = CC.Major AND CA.Minor = CC.Minor
     GROUP BY CA.Name, CA.Major, CA.Minor;
    

    Tested – it works and produces the same answer as Andomar‘s query does.


    Performance

    I created a bigger volume of data (11616 rows of data), and ran a benchmark timing of Andomar’s query against mine – target DBMS was IBM Informix Dynamic Server (IDS) version 11.70.FC2 running on MacOS X 10.7.2. I used the first of Andomar’s two queries since IDS does not support the comparison notation in the second one. I loaded the data, updated statistics, and ran the queries both with mine followed by Andomar’s and with Andomar’s followed by mine. I also recorded the basic costs reported by the IDS optimizer. The result data from both queries were the same (so the queries are both accurate – or equally inaccurate).

    Table unindexed:

    Andomar's query                           Jonathan's query
    Time: 22.074129                           Time: 0.085803
    Estimated Cost: 2468070                   Estimated Cost: 22673
    Estimated # of Rows Returned: 5808        Estimated # of Rows Returned: 132
    Temporary Files Required For: Order By    Temporary Files Required For: Group By
    

    Table with unique index on (name, major, minor, revision):

    Andomar's query                           Jonathan's query
    Time: 0.768309                            Time: 0.060380
    Estimated Cost: 31754                     Estimated Cost: 2329
    Estimated # of Rows Returned: 5808        Estimated # of Rows Returned: 139
                                              Temporary Files Required For: Group By
    

    As you can seen, the index dramatically improves the performance of Andomar’s query, but it still seems to be more expensive on this system than my query. The index gives a 25% time saving for my query. I’d be curious to see comparable figures for the two versions of Andomar’s query on comparable volumes of data, with and without the index. (My test data can be supplied if you need it; there were 132 products – the 3 listed in the question and 129 new ones; each new product had (the same) 90 version entries.)

    The reason for the discrepancy is that the sub-query in Andomar’s query is a correlated sub-query, which is a relatively expensive process (dramatically so when the index is missing).

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

Sidebar

Related Questions

Possible Duplicate: SQL Server: Only last entry in GROUP BY I have a table
Possible Duplicate: SQL query to find Missing sequence numbers I have a table which
Possible Duplicate: T-SQL Pivot? Possibility of creating table columns from row values I've been
Possible Duplicate: dynamic sql pivot in sql server I have a table called Col_values
Possible Duplicate: SQL - find records from one table which don't exist in another
Possible Duplicate: SQL Server Database query help Hi, I have a problem that I
Possible Duplicate: LINQ to SQL: Return anonymous type? I have a standard LINQ to
Possible Duplicate: Split string in SQL I have seen a couple of questions related
Possible Duplicate: Best way to stop SQL Injection in PHP I have seen some
Possible Duplicate: Can you define “literal” tables in SQL? Occasionally I find myself in

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.