Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    tweety is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Nov 2012
    Posts
    126

    Group results to show last sale

    I have a working system (sales and stock) i would like the system to show me when a customer placed their last order.

    i have made a query to show the shop name (from the customer table) and the date of sale (from the sales table)

    the query works but shows all the sales carried out. so if one shop has 10 sales the query will display that shop name 10 tens, whereas i would only like to show the last or lastest sale.

    I have made a report based on this query - maybe its more easy to look at the attachment
    Click image for larger version. 

Name:	LastCustomerSale.png 
Views:	10 
Size:	6.2 KB 
ID:	11960



    any ideas anyone? Thanks
    Last edited by tweety; 04-18-2013 at 08:25 AM. Reason: More Detailed

  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,641
    You may just want the first query:

    http://www.baldyweb.com/LastValue.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    SealM is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Aug 2012
    Posts
    40
    Assuming your table is called [shop], here is the query that would show ONLY the last date:

    SELECT [shop].[shop name], Max([shop name].[date of sale]) AS [MaxOfdate of sale]
    FROM [shop]
    GROUP BY [shop].[shop name];

  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,641
    Quote Originally Posted by SealM View Post
    Assuming your table is called [shop], here is the query that would show ONLY the last date:

    SELECT [shop].[shop name], Max([shop name].[date of sale]) AS [MaxOfdate of sale]
    FROM [shop]
    GROUP BY [shop].[shop name];
    No, that would throw a parameter prompt.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    SealM is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Aug 2012
    Posts
    40
    I am so sorry. Here is the correction...

    SELECT shop.[shop name], Max(shop.[date of sale]) AS [MaxOfdate of sale]
    FROM shop
    GROUP BY shop.[shop name];

  6. #6
    tweety is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Nov 2012
    Posts
    126
    Thank you for your help, i couldnt apply the sql given so i used the max from the totals

    i noted while testing, if a customer has never placed an order, then their name is not listed at all in the report

    how do i go about that?

  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,641
    Try to add your customer table to the query, and edit the join appropriately.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    SealM is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Aug 2012
    Posts
    40
    Can you post the SQL of your query?

  9. #9
    tweety is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Nov 2012
    Posts
    126
    i used the design view to get in running as iam not good with coding, but the sql in below

    SELECT tblCustomer.ShopName, Max(tblSales.DateofSale) AS MaxOfDateofSale
    FROM tblCustomer INNER JOIN tblSales ON tblCustomer.CustomerID = tblSales.CustomerID
    GROUP BY tblCustomer.ShopName;

    Click image for larger version. 

Name:	1.png 
Views:	3 
Size:	121.1 KB 
ID:	11994

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Change INNER to LEFT and see what happens.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    tweety is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Nov 2012
    Posts
    126
    thanks that has sloved the problem, it now shows a customer who has never placed an order. IN the report, this is no date (as there is no sale ever made) is there a way to display a message saying to "no sale made"

    May i ask what the difference between inter and left is please

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Check out the Nz() function for the no date issue.

    If you double-click on the join line between the tables in design view, you'll get a good description of what the 3 options do (LEFT, RIGHT and INNER).
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    tweety is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Nov 2012
    Posts
    126
    I found this on the web, but where do i place it? under which event?

    If Me![MaxOfDateofSale] = Null Then
    MaxOfDateofSale= ("No sales made")
    Exit Sub
    End If

  14. #14
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Well, it wouldn't work, but if it did you'd want the detail format event. Did you not like the Nz() function?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  15. #15
    tweety is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Nov 2012
    Posts
    126
    the nz fucation requires coding (which i dont really like) however i came accross this http://www.techonthenet.com/access/f...dvanced/nz.php
    is that what i should be using or applying? should i be add a variable in the in the query like
    expr1: nz([MaxOfDateofSale],'No Sale Made')

    thanks

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 06-21-2012, 07:40 PM
  2. Replies: 4
    Last Post: 05-25-2012, 09:43 AM
  3. Replies: 6
    Last Post: 05-14-2012, 07:24 AM
  4. Group by pulling incorrect results
    By jpawson74 in forum Queries
    Replies: 7
    Last Post: 03-28-2012, 09:46 AM
  5. Group query results
    By shak2 in forum Access
    Replies: 7
    Last Post: 10-20-2010, 02:41 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