Results 1 to 10 of 10
  1. #1
    toddaway is offline Novice
    Windows XP Access 2003
    Join Date
    May 2013
    Posts
    5

    How do I show only 3 records per customer?

    Sorry if this is the wrong place to post...maybe in reports?



    Ultimately, I need a report that shows CUSTOMER NAME, DATE1, DATE2, DATE3

    For simplicity, say the main table just contains 2 customers and the dates they made purchases, 1 entry for each date. So Smith has 20 dates that he made purchases and Jones has 15 dates.

    It is easy enough to run a query with "Smith" as a criterion and limit the results to 3 (I just need the last 3 dates). But then how would I make the report list those 3 dates in a row as opposed to 1 per page?

    Also, what if I had 100 employees and I didn't want to make a query for each one with their last name as the criterion?

    Thanks.

  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,652
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    toddaway is offline Novice
    Windows XP Access 2003
    Join Date
    May 2013
    Posts
    5
    Thanks, pbaldy.

  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,652
    No problem, and welcome to the site by the way!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    toddaway is offline Novice
    Windows XP Access 2003
    Join Date
    May 2013
    Posts
    5
    So I've been trying to get this to work with my specific database, and I just can't wrap my head around it for some reason. My brain gets lost in the subquery. Maybe because I'm working with 2 tables and a query and the code gets confusing. Anyway, from the site you linked to:

    Code:
    SELECT Orders.CustomerID, Orders.OrderDate, Orders.OrderID
    FROM Orders
    WHERE Orders.OrderID IN
       (SELECT TOP 3 OrderID                            
       FROM Orders AS Dupe                              
       WHERE Dupe.CustomerID = Orders.CustomerID        
       ORDER BY Dupe.OrderDate DESC, Dupe.OrderID DESC) 
    ORDER BY Orders.CustomerID, Orders.OrderDate, Orders.OrderID;
    I don't understand
    Code:
     WHERE Dupe.CustomerID = Orders.CustomerID
    It's like saying where 1 = 1, so why bother? Or why not use any field from the table since they will all be equal?
    It seems like it could just be SELECT TOP 3 FROM <the whole table sorted by date>.

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    You are using the same table (Orders) twice - once as Orders and once as Dupes. The database needs the alias to keep things organized.

    You're getting the TOP 3 Orderids based on OrderDate desc and OrderId desc
    Then you are getting the other info based on these OrderIds.

    I'm sure there are more elegant explanations, but that's what it's saying to me.
    Hope it helps.

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Sorry, missed the follow up question. Orange explained it as well as or better than I could. All I can say is it works. I use this method in a car maintenance db among others, and it works flawlessly.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    toddaway is offline Novice
    Windows XP Access 2003
    Join Date
    May 2013
    Posts
    5
    I understand that it's getting results from results, I just don't understand why it needs to reference the table twice in the subquery anyway. It's not like it needs to find a previous record to compare a current record to, like an explanation I saw somewhere. I guess I'm too thick. But I do appreciate you trying to clarify things for me.

  9. #9
    toddaway is offline Novice
    Windows XP Access 2003
    Join Date
    May 2013
    Posts
    5
    Well, I figured out a working SQL statement. It does not use the "alias" method as given in the example. I got it from method 1 on this page: http://support.microsoft.com/kb/153747

  10. #10
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Glad you have it sorted out.
    How about showing us the final query you used, and the data in and out?
    Someone else may benefit from what you have found.

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

Similar Threads

  1. Replies: 1
    Last Post: 12-24-2011, 08:48 AM
  2. Replies: 1
    Last Post: 10-26-2011, 05:13 AM
  3. Replies: 4
    Last Post: 08-18-2011, 01:52 PM
  4. Replies: 1
    Last Post: 06-03-2011, 04:17 AM
  5. Show all records.
    By dennisvillareal in forum Access
    Replies: 1
    Last Post: 03-03-2011, 04:28 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