Results 1 to 5 of 5
  1. #1
    MaxQTime is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Apr 2019
    Location
    Kelowna, B.C.
    Posts
    42

    Customers Based On Last Order Date

    Hi Everyone, I was looking to pull a list of Dealers based on when their last sale was. I have the DB set up with multiple tables, the 2 tables that are relevant to this is the Dealer table and the Orders Table. Each order is linked to a specific dealer and dealers can have 0, 1 or many orders. Each order has a date ordered field with it.

    I need to be able to pull a list of dealers who have ordered from us within the last year and the last 6 months. Is there a way to do this with a single query or is it going to get more complicated?

    I'm having a bit of trouble trying to wrap my head around how to do this.

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    you just need one query. Join the two tables on dealerID and use criteria on the order date. Not sure what your criteria needs to be since any orders in the last 6 months will also be within the last year - perhaps provide some examples of what you mean. SQL might look something like

    Code:
    SELECT DISTINCT tblDealers.*
    FROM tblDealers INNER JOIN tblOrders ON tblDealers.DealerPK=tblOrders.DealerFK
    WHERE OrderDate>Dateadd("yyyy",-1,Date())

  3. #3
    Join Date
    Apr 2017
    Posts
    1,792
    On fly:
    Code:
    SELECT DISTINCT d.*, lo.LastOrder
    FROM
    tblDealers d
    INNER JOIN
    (SELECT o.DealerFK, MAX(o.OrderDate) AS LastOrder FROM tblOrders o GROUP BY o.DealerFK WHERE o.OrderDate >= DateAdd(mm, -6, DateSerial(Year(Date),Month(Date),1)))) lo ON lo.DealerFK = d.DealerPK
    Of course you may have a saved query instead of subquery.

  4. #4
    MaxQTime is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Apr 2019
    Location
    Kelowna, B.C.
    Posts
    42
    Thank you, the SELECT DISTINCT and MAX was exactly what I was looking for.

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870

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

Similar Threads

  1. Orders to Be Automatically Grouped Based on Order Date
    By hellocng in forum Database Design
    Replies: 2
    Last Post: 10-25-2017, 02:12 AM
  2. Replies: 3
    Last Post: 02-09-2016, 04:36 PM
  3. Replies: 1
    Last Post: 06-02-2015, 10:07 AM
  4. Replies: 7
    Last Post: 06-04-2013, 11:14 AM
  5. Replies: 6
    Last Post: 12-07-2012, 07:57 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