Results 1 to 11 of 11
  1. #1
    Collizz is offline Novice
    Windows 8 Access 2013
    Join Date
    Dec 2013
    Posts
    6

    Selecting single (highest) instance of joined tables

    Hi,

    I have returned to Access and SQL after some time away and cannot for the life of me remember how to do something which should be easy enough, if only my little grey cells were operating efficiently ...

    I have two tables; one for documents, the other for versions of those documents (simplified field structure):

    tblDocs - docNum [PK], docName
    tblVers - docNum + versNum + revNum [PK], lastModDate

    So, given the following tblDocs data:

    1001 | Document One
    1002 | Document Two
    1003 | Document Three

    and the related tblVers data (shown in descending key sequence):

    1001 | 1 | 2 | 11/12/13 <
    1001 | 1 | 1 | 09/12/13
    1001 | 0 | 1 | 01/01/13
    1002 | 1 | 1 | 11/11/13 <
    1003 | 2 | 0 | 12/12/13 <


    1003 | 1 | 1 | 02/02/13

    How do I end up with a result set containing just the latest issues (indicated above with '<')?

    I've tried DISTINCT, MAX, etc. and believe it is some kind of JOIN structure I may need?

    Thanks a lot for any 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
    Does this work for you?

    http://www.baldyweb.com/LastValue.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Collizz is offline Novice
    Windows 8 Access 2013
    Join Date
    Dec 2013
    Posts
    6
    Paul,

    Thanks for the suggestion, but I don't think you grasped what I'm looking to do.

    I have two related tables, not one, as stated.

    The query I have now returns the following (using my example data):

    1001 | 1 | 2 | 11/12/13 <
    1001 | 1 | 1 | 09/12/13
    1001 | 0 | 1 | 01/01/13
    1002 | 1 | 1 | 11/11/13 <
    1003 | 2 | 0 | 12/12/13 <
    1003 | 1 | 1 | 02/02/13

    I am looking to see only the rows I have arrowed above (3 rows in total), comprising the primary key from tblDocs (e.g. 1001) and the highest version + revision (e.g. 1 + 2) from tblVers:

    1001 | 1 | 2 | 11/12/13
    1002 | 1 | 1 | 11/11/13
    1003 | 2 | 0 | 12/12/13

    Any other ideas?

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I still think that technique will work. Can you post a sample db here?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Collizz is offline Novice
    Windows 8 Access 2013
    Join Date
    Dec 2013
    Posts
    6

    One-to-many relationship, requiring highest match on primary key

    Quote Originally Posted by pbaldy View Post
    I still think that technique will work. Can you post a sample db here?
    It really is pretty simple. I have one table, tblDocs, keyed on docNum (set to Required=Yes, Indexed = Yes, no duplicates). I have a second table, tblVers, keyed on docNum+versNum+revNum (all 3 are set to Required=Yes, Indexed = Yes, Duplicates OK)

    I want to end up with the following 3 records in my result set (Document Number + Highest Version Number + Revision Number | Last Mod Date, etc.):

    1001 | 1 | 2 | 11/12/13 ...
    1002 | 1 | 1 | 11/11/13 ...
    1003 | 2 | 0 | 12/12/13 ...

    Can anyone tell me how I can achieve? It would seem to be a typical query, after all.

    Thanks

  6. #6
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I'd do this in two queries

    1.

    SELECT DocNum, Max(VersNum) as MaxVers FROM tblVers GROUP BY DocNum

    This should give you a list of the highest version number for each docnum

    2.

    Link this query to your version table through the docnum and versnum, make it an aggregate query (look for the sigma button on the toolbar, looks like a capital E) and put MAX in the revnum field

    ALTERNATELY

    If you are 100% certain that your revision date is being recorded correctly you can perform the same type of query using the date field.

  7. #7
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    One more note, the MAX function will only work if the version and revision numbers are actually numbers and not text fields. You may get inconsistent results if you are using max/min on text field that looks numeric but you can handle that with the clng or cint function if the field is always numeric in appearance. If you start to use 1a, 2b (alpha and numeric) values you may have to fiddle with it a bit more.

  8. #8
    Collizz is offline Novice
    Windows 8 Access 2013
    Join Date
    Dec 2013
    Posts
    6
    Quote Originally Posted by rpeare View Post
    One more note, the MAX function will only work if the version and revision numbers are actually numbers and not text fields. You may get inconsistent results if you are using max/min on text field that looks numeric but you can handle that with the clng or cint function if the field is always numeric in appearance. If you start to use 1a, 2b (alpha and numeric) values you may have to fiddle with it a bit more.
    Thanks, rpeare, I'll try that. It's so crazy, it might just work ...

    All "...Num" fields are indeed numeric, so I'll give it a whirl.

  9. #9
    Collizz is offline Novice
    Windows 8 Access 2013
    Join Date
    Dec 2013
    Posts
    6
    Hi rpeare

    I can't see any indication of an aggregate function; the only sigma sign is a standard 'total' function.

    Can you please let me know what format the SQL statement needs to take, to end up with the result set I'm looking for in my original posting? My VBA code sets the listbox source to a SQL query, which works fine, but returns all docNum+versNum+revNum combinations. I just need the SQL to bring in only the latest tblDocVers.modDate valued row per docNum+versNum+revNum.

    Thanks

  10. #10
    Collizz is offline Novice
    Windows 8 Access 2013
    Join Date
    Dec 2013
    Posts
    6
    Paul

    I just wanted to say 'thanks' for your help. I did use the double query to produce the list I need. I just need now to figure out how to make the listbox source equal to a combination of the two queries ...

    Also, if somebody can tell me how to mark this post 'closed' it would be appreciated!

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help. I would think you could make the second query the listbox row source. You can mark the thread solved in a Thread Tools at the top of the thread.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Selecting highest value across several fields
    By megabrown in forum Queries
    Replies: 19
    Last Post: 10-28-2014, 01:16 AM
  2. Replies: 5
    Last Post: 10-11-2013, 07:29 AM
  3. Return only one instance of a single field
    By runtheeast in forum Queries
    Replies: 1
    Last Post: 08-13-2013, 04:19 PM
  4. Replies: 3
    Last Post: 04-05-2012, 08:33 AM
  5. Multiple Tables Joined ????
    By Ekim in forum Database Design
    Replies: 10
    Last Post: 01-15-2012, 07:08 PM

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