Results 1 to 10 of 10
  1. #1
    murfeezlaw is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    26

    Group by query with Maximum value

    I'm trying to build a Group By query to pull the record with the latest value of the Version.



    CompanyID can vary. I want the record with the latest value of Version which will have a value of A, B, C etc...

    For example I would only want to pull the record with Version D showing me CompanyID RM0060

    QuoteID Version CompanyID
    0800323 D RM0060
    0800323 C RM0060
    0800323 B RM0060
    0800323 A RM0012


    Any ideas? I tried using the DMax function and it seems to just hang. It takes a long time since it's a big table.

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    It takes a long time since it's a big table.
    and probably not indexed? Actually I don't think dmax uses indexing anyway and not sure why you need a groupby query to return a single record

    try something like

    Code:
    SELECT TOP 1 *
    FROM myTable
    WHERE QuoteID='0800323'
    ORDER BY Version Desc

  3. #3
    murfeezlaw is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    26
    Thanks! that's really close. How would I modify that so that I don't specify a QuoteID? There are many records with the same QuoteID and different Versions.

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    you mean you want the max version for all quoteID's? If so then you need a different query

  5. #5
    murfeezlaw is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    26
    Yes, exactly. I narrowed the records and indexed the table but it still takes a little bit. Do you have any idea how to run it differently?

  6. #6
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    then you need this query - note the aliasing

    Code:
    SELECT O.*
    FROM myTable AS O INNER JOIN (SELECT QuoteID, max(Version) AS Latest FROM myTable GROUP BY QuoteID) AS L
    ON O.QuoteID=L.QuoteID AND O.Version=L.Version

  7. #7
    murfeezlaw is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    26
    Thank you very much. So O=mytable or dbo_Version. I'm getting an error on L though. What is L an alias for?

  8. #8
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    L is alias for the subquery (the bit in brackets). Aliases can be whatever you want within normal naming conventions, in this case, for me O stands for 'Objective Data' and L for 'Latest Data'. Could just as easily used X and Y or Target and Maxversion, just something that makes sense to you. I use them all the time to make sql easier to read and cut down on typing.

    With regards your error - what is it? and copy and paste the sql you have actually used that generates the error.

  9. #9
    murfeezlaw is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    26
    Right, that's what I thought. Sorry my SQL is a little weak.

    Here is my SQL code:
    SELECT O.*
    FROM Version AS O INNER JOIN (SELECT QuoteID, max(Version) AS Latest FROM Version GROUP BY QuoteID) AS L
    ON O.QuoteID=L.QuoteID AND O.Version=L.Version

    I'm not actually getting an error now. It's prompting me for O.Version and L.Version

    Thank you so much for your time!

  10. #10
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    Version is either a table name or a field name, it can't be both. You mentioned dbo_version before to replace highlighted in red?

    FROM Version AS O INNER JOIN (SELECT QuoteID, max(Version) AS Latest FROM Version

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

Similar Threads

  1. Replies: 5
    Last Post: 07-14-2015, 08:53 AM
  2. Replies: 6
    Last Post: 06-09-2015, 09:51 AM
  3. Replies: 2
    Last Post: 07-24-2014, 03:37 PM
  4. Query to extract maximum figures for each date
    By vijanand1279 in forum Queries
    Replies: 2
    Last Post: 11-03-2011, 10:41 PM
  5. Replies: 2
    Last Post: 06-22-2010, 07:09 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