Results 1 to 4 of 4
  1. #1
    aero is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    12

    Exclamation query to return date field corresponding to Max(Quantity)

    I'm having trouble writing a query that will give me the RPT_PERIOD field corresponding to the result of the MAX Quantity. Whenever I put it in the same query it ends up giving me a max for each Report Period. Anyone know what to do? Do I need to do a subquery and if so what would that be?

    Query to determine Average:

    SELECT table.PROD_CODE, table.PT_SP_TYPE, MAX(table.QUANTITY) AS MAXOfQUANTITY
    FROM table
    WHERE (((table.RESP_ID)=[Forms]![Selection Form]![RID]) AND ((table.RPT_PERIOD)>'140100'))


    GROUP BY table.PROD_CODE, table.PT_SP_TYPE;

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Yes, you can do this in a series of two queries (or use a SubQuery).

    The first query should just return the Max Quantity and the other key fields (that you are Grouping By).
    Then, create a new query with joins the 1st query to your original data table based on your key fields and Quantity, and then you can return the fields that you want from the data table.

  3. #3
    aero is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    12
    I need the second to join the info from the first Query "Respondent ID", Product Code", "Product Type (3 per code)", and max("Quantity") for each combination of the fields that came before. The additional field I need this query to add on is the reportreport period for which the selected ID has a maximum quantity of each product code and product type. I'm new to SQL and MS Access. Can anyone provide the sql statement or tell me what needs to go in query design in access?

  4. #4
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    You already pretty much most of the first query already written. So, something like this:

    Query1
    Code:
    SELECT table.PROD_CODE, table.PT_SP_TYPE, MAX(table.QUANTITY) AS MAXOfQUANTITY
    FROM table
    WHERE table.RPT_PERIOD>'140100'
    GROUP BY table.PROD_CODE, table.PT_SP_TYPE;
    Query 2
    Code:
    SELECT table.PROD_CODE, table.PT_SP_TYPE, table.RPT_PERIOD, Query1.MAXOfQUANTITY
    FROM table
    INNER JOIN Query1
    ON (table.PROD_CODE=Query1.PROD_CODE) AND (table.PT_SP_TYPE=Query1.PT_SP_TYPE) AND (table.QUANTITY=Query1.MAXOfQUANTITY)
    WHERE table.RESP_ID=[Forms]![Selection Form]![RID];

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

Similar Threads

  1. Replies: 3
    Last Post: 04-01-2012, 01:40 PM
  2. Query to return only the row containing the highest date.
    By eric.opperman1@gmail.com in forum Queries
    Replies: 4
    Last Post: 03-22-2011, 08:42 PM
  3. Replies: 1
    Last Post: 03-01-2011, 04:03 PM
  4. Query return for date ranges.
    By Desstro in forum Queries
    Replies: 2
    Last Post: 09-14-2010, 06:44 AM
  5. Return blank field depending on quantity
    By anthonyjf in forum Access
    Replies: 1
    Last Post: 04-01-2009, 08:22 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