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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:04:03+00:00 2026-05-30T21:04:03+00:00

I am trying to join 2 tables, but only join with the latest record

  • 0

I am trying to join 2 tables, but only join with the latest record in a group of records.

The left table:

Part

  • Part.PartNum

The right table:

Material

  • Material.Partnum
  • Material.Formula
  • Material.RevisionNum

The revision number starts at “A” and increases.

I would like to join the 2 tables by PartNum, but only join with the latest record from right table. I have seen other examples on SO but an having a hard time putting it all together.

Edit:

I found out the first revision number is “New”, then it increments A,B,… It will never be more than one or two revisions, so I am not worried about going over the sequence. But how do I choose the latest one with ‘New’ being the first revision number?

  • 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-30T21:04:04+00:00Added an answer on May 30, 2026 at 9:04 pm

    A general SQL statement that would run this would be:

    select P.PartNum, M.Formula, M.RevisionNum
    from Parts P
    join Material M on P.PartNum = M.PartNum
    where M.RevisionNum = (select max(M2.RevisionNum) from Material M2
                           where M2.PartNum = P.PartNum);
    

    Repeating the above caveats about what happens after Revision #26. The max(RevisionNum) may break depending upon what happens after #26.


    EDIT:

    If RevisionNum sequence always starts w/ NEW and then continues, A, B, C, etc., then the max() needs to be replaced w/ something more complicated (and messy):

    select P.PartNum, M.RevisionNum
    from Parts P
    join Material M on P.PartNum = M.PartNum
    where (
          (select count(*) from Material M2 
                  where M2.PartNum = P.PartNum) > 1
          and M.RevisionNum = (select max(M3.RevisionNum) from Material M3
                       where M3.PartNum = P.PartNum and M3.RevisionNum <> 'NEW')
          )
          or ( 
          (select count(*) from Material M4
                  where M4.PartNum = P.PartNum) = 1
           and M.RevisionNum = 'NEW'
          )
    

    There must be a better way to do this. This works though — will have to think about a faster solution.

    SQL Fiddle: http://sqlfiddle.com/#!3/70c19/3

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

Sidebar

Related Questions

I'm trying to join multiple tables, but one of the tables has multiple records
I'm trying to join two table (call them table1 and table2) but only return
i'm trying to join 3 tables together but use an IF statment to only
I am trying to LEFT JOIN 2 tables. which is working out fine. But
I'm trying to LEFT JOIN two tables, to get a list of all rows
I have a MySQL Left Join problem. I have three tables which I'm trying
I'm trying to create a record within a join table from the action of
I'm trying to create a record within a join table from the action of
I'm trying to left join a table where all topics are located. What im
I am trying to join three tables together but having a problem getting the

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.