Q
Problem solve Get help with specific problems with your technologies, process and projects.

Highest revision within highest version

I would like to extract for a particular part number what is the highest version and revision and corresponding serial number.

I would like to extract for a particular part number what is the highest version and revision, and corresponding...

serial number.

SLNO    PARTNO  VER  REVISION
P00026  1265     A     1.1
P00027  1265     A     1.1.1
P00028  1265     B     1
P00029  1265     B     1.1
P00030  1270     A     1.2
P00031  1270     A     1.3

For example, part number 1265 has B as highest version and 1.1 as highest revision of B, so it should show like this...

SLNO    PARTNO  VER  REVISION
P00029  1265     B     1.1

How do I do this in SQL? Thank you.

Nice question. Here's one approach:

select P.slno
     , P.partno
     , P.ver
     , P.revision
  from (
       select partno
            , max(ver) as maxver
         from parts
       group
           by partno
       ) as V
inner
  join (
       select partno
            , ver
            , max(revision) as maxrevision
         from parts
       group
           by partno
            , ver
       ) as R
    on R.partno = V.partno
   and R.ver    = V.maxver
inner
  join parts as P
    on P.partno   = R.partno
   and P.ver      = R.ver
   and P.revision = R.maxrevision

Note that this will fail if any of your revision or subrevision numbers goes beyond a single digit. Remember that string columns sort from left to right, not numerically, so here's how they would sort if there were 12 revisions:

1.1
1.10
1.11
1.12
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9

Of course, the query would still be correct! However, you wouldn't get what you thought was the highest (1.12), you'd get what actually was the highest (1.9).

This was last published in April 2007

Dig Deeper on Oracle and SQL

PRO+

Content

Find more PRO+ content and other member only offers, here.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide.com

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close