Results 1 to 7 of 7
  1. #1
    ironfelix717 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    150

    MS Access - INNER Join Group By Max - Without Inner Join?

    Hi all,

    Basically, I am querying data which has a record version field. The max verison number is the latest record. I need to display all data in the table associated with the latest version.

    I've been able to do this with the help of this link: https://www.techonthenet.com/access/...max_query2.php



    This method requires a "double query", if you will. It needs an intermediate query that is joined to the final query.

    I was wondering if there is any way to eliminate the need for this double query, where I can query this data with one single SQL statement.

    I've attached an example where you can see the data and results. JoinMaxQuery.accdb.zip


    Thanks for your help!

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The first query can be a subquery:

    SELECT...
    FROM TableName INNER JOIN (SELECT...) ON...

    but personally I find them difficult to work with that way, at least in Access. The design grid can do funky things to subqueries.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    You can nest queries. Copy/paste the first query statement into the second. Use SQLView of query builder:

    SELECT Table1.ID, Table1.Version, Table1.Val, Table1.Val2
    FROM Table1 INNER JOIN (SELECT Table1.ID, Max(Table1.Version) AS MaxOfVersion
    FROM Table1 GROUP BY Table1.ID) AS
    Query1 ON (Table1.Version = Query1.MaxOfVersion) AND (Table1.ID = Query1.ID);

    Here's another query approach:

    SELECT * FROM Table1 WHERE Record IN (SELECT TOP 1 Dupe.Record FROM Table1 AS Dupe WHERE Dupe.ID=Table1.ID ORDER BY Dupe.Version DESC);

    Review: http://allenbrowne.com/subquery-01.html#TopN
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    ironfelix717 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    150
    June7 & pbaldy,

    Those solutions will work and work nicely! Especially the Allen Browne approach. Great, this makes my project much cleaner!

    Thank you so much.

  5. #5
    ironfelix717 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    150
    June7,

    Is there any way I can choose which fields I want to return from the Allen Browne query?

    If I ONLY want ID, Val1, and Val2 fields to return in the query, how might I still use his approach?

    Thanks

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Yes, you can explicitly reference field names instead of * wildcard. His example shows that. I just went the lazy route.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    ironfelix717 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    150
    Silly me, I missed the asterisk somehow! Thanks a bunch!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 09-20-2017, 09:50 AM
  2. Replies: 6
    Last Post: 11-19-2013, 01:38 AM
  3. Inner Join works but not Left Join
    By kwooten in forum Queries
    Replies: 1
    Last Post: 12-11-2012, 05:09 PM
  4. Outer Join Nested in Inner Join
    By Stevens7 in forum Queries
    Replies: 2
    Last Post: 10-19-2011, 01:34 PM
  5. SQL query with SUM, INNER JOIN and GROUP BY
    By BayerMeister in forum Queries
    Replies: 2
    Last Post: 08-14-2010, 12:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums