Results 1 to 7 of 7
  1. #1
    Sprtrmp01 is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    May 2019
    Posts
    1

    subquery to return most recent date per customer.

    I haven't been able to find exactly what I am looking for in older forum posts (at least not anything that I can get to work), so I figured I would see if anyone can help me out.



    I have 2 pretty simple tables. the first is a "customers" table that just has info related to customers: CustomerID_PK, CustomerName, etc
    the second table holds a list of order dates (and other info, but for this question, I am only concerned with dates): OrderID_PK, CustomerID_FK (related to PK on "customers" table), OrderDate, etc. each customer has multiple orders, and this table really only holds a list of dates.

    What I need to do is build a query that returns the customerID_PK and CustomerName (from "customers" table) and the MOST RECENT order date for EACH CUSTOMER. in other words, I need the most recent order date for CUST001, the most recent order date for CUST002... and so on. no other dates, just the most recent date for EACH customer.

    so far, in query design view, I have
    CustomerID_PK (sorted in ascending order)
    CustomerName
    OrderDate (sorted in descending order)

    and I need to put a sub-query somewhere that would look at either the customerID_PK field or the OrderDate field and eliminate all the the first (most recent date)

    not much experience with SQL or sub-queries, so any help is greatly appreciated.

    Thanks in advance

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Use an aggregate query or if you want to return all fields, use TOP N nested query. 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.

  3. #3
    sprtrmp is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Sep 2015
    Location
    Greenville, SC
    Posts
    102
    when I use the top n query, I only get back the FIRST record. only one out of hundreds.
    I am looking for the first date for each customer. I'm sure I'm doing something wrong, but not sure what.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    The referenced link is an example of your exact situation. What have you attempted? Post code such as SQL statement.

    If you only want last order date, then use an aggregate query.

    SELECT CustomerID_PK, CustomerName, Max(OrderDate) AS MaxDate FROM Orders INNER JOIN Customers on CustomerID_PK=CustomerID_FK GROUP BY Customer_ID, CustomerName;

    Or build a report and use its Sorting & Grouping features with aggregate calcs. Report allows display of detail data as well as summary calcs.
    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.

  5. #5
    sprtrmp is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Sep 2015
    Location
    Greenville, SC
    Posts
    102
    here is the basic SQL statement and I have not added anything for the subquery.

    SELECT tblCust.CustID_PK, tblCust.LName, tblOrder.OrderDate
    FROM tblCust INNER JOIN tblOrder ON tblCust.CustID_PK = tblOrder.CustID_FK
    ORDER BY tblCust.CustID_PK, tblOrder.OrderDate DESC;

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    That does not follow either example provided.

    Try the aggregate in post 5.
    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
    sprtrmp is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Sep 2015
    Location
    Greenville, SC
    Posts
    102
    I have been poking around online, and I came across the following:

    http://www.databasedev.co.uk/access_max_function.html

    Was a tremendous help and it described pretty much the exact problem I was trying to solve. Works PERFECTLY!!

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

Similar Threads

  1. Replies: 6
    Last Post: 09-08-2016, 11:04 AM
  2. return most recent records by date stored as text
    By mindbender in forum Queries
    Replies: 3
    Last Post: 04-27-2016, 07:00 AM
  3. Replies: 5
    Last Post: 09-22-2015, 12:18 PM
  4. Replies: 1
    Last Post: 08-25-2011, 09:50 AM
  5. Return most recent entry for each item
    By GenAp in forum Queries
    Replies: 1
    Last Post: 02-04-2010, 05:30 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