Results 1 to 10 of 10
  1. #1
    dylan_dog is offline Novice
    Windows XP Access 2003
    Join Date
    Jan 2012
    Posts
    9

    average purchase value between two dates


    Hi all,
    I have a problem with a query. I have two tables, the first with customer ID's and contact data and related table with purchases for each customer. The second table has customer ID, date of purchase and amount.What I try to do is to display all the customers within given date range with average purchase above custom minimum. Purchase amount in my query is grouped by average. Query works fine regarding time frame, my problem is that I don't get just one single average value for each customer that meets the criteria. What I'm getting is a list of all the purchases for every customer that exceeds minimum value. I also un-ticked date field in query design so dates don't show when I run the query. How can I get just that one average value for each customer?

    Regards, Samo

  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
    What's the SQL of the query? It sounds like you're still grouping on the date field. It should just be in the WHERE clause (or nowhere).
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    dylan_dog is offline Novice
    Windows XP Access 2003
    Join Date
    Jan 2012
    Posts
    9
    SELECT baza2.ID, baza2.Ime, baza2.Priimek, tblTHW.datum, Avg(tblTHW.THW) AS AvgOfTHW
    FROM baza2 INNER JOIN tblTHW ON baza2.ID = tblTHW.ID
    GROUP BY baza2.ID, baza2.Ime, baza2.Priimek, tblTHW.datum
    HAVING (((tblTHW.datum) Between [forms]![frmIznadX]![txtDatumZadnjegTrekanjaOd] And [forms]![frmIznadX]![txtDatumZadnjegTrekanjaDo]) AND ((Avg(tblTHW.THW))>[forms]![frmIznadX]![txtTHWIznad]));


    datum means date, THWIznad is the target min average value, Ime is name, Priimek is second name and the other two are dates

  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
    As I suspected:

    SELECT baza2.ID, baza2.Ime, baza2.Priimek, tblTHW.datum, Avg(tblTHW.THW) AS AvgOfTHW
    FROM baza2 INNER JOIN tblTHW ON baza2.ID = tblTHW.ID
    GROUP BY baza2.ID, baza2.Ime, baza2.Priimek, tblTHW.datum

    Having it in the GROUP BY clause is telling Access you want to see it grouped on date.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    dylan_dog is offline Novice
    Windows XP Access 2003
    Join Date
    Jan 2012
    Posts
    9
    First, thank you for the answers. I tried to remove group by date in SQL view and it replaced "group by" with "expression" in design view and the query won't run. How do I remove date from grouping?

    Regards, Samo

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    It appears you need it in there for the criteria, so change Group By to Where in design view.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    dylan_dog is offline Novice
    Windows XP Access 2003
    Join Date
    Jan 2012
    Posts
    9
    It doesn't run :-). Maybe the easiest solution would be to make two queries, the first to just filter by date and the second based on the first to calculate averages.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    If you're still stuck, post a sample db here we can play with and sort out the problem.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    dylan_dog is offline Novice
    Windows XP Access 2003
    Join Date
    Jan 2012
    Posts
    9
    Hi,

    I tried with two queries and it works fine. Than I found this and it does exactly what I need:


    Code:
    SELECT 
        tblPurchase.Customer_ID
        ,tblCustomer.customerName
        ,Avg(tblPurchase.Amount) AS AvgOfAmount
    FROM 
        tblCustomer 
        INNER JOIN 
        tblPurchase 
        ON tblCustomer.customerID = tblPurchase.Customer_ID
    WHERE 
        (((tblPurchase.DateOfPurchase) Between #1/1/2012# And #1/10/2012#))
    GROUP BY 
        tblPurchase.Customer_ID, 
        tblCustomer.customerName
    HAVING 
        (((Avg(tblPurchase.Amount))>200));
    Thank you

  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
    Glad you got it sorted out.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Query to average between two dates.
    By stormchaser in forum Queries
    Replies: 4
    Last Post: 12-15-2011, 03:06 PM
  2. Purchase Order Report
    By jordanturner in forum Forms
    Replies: 1
    Last Post: 10-13-2010, 10:53 PM
  3. Purchase price of asset
    By Ron.Sul in forum Access
    Replies: 4
    Last Post: 09-17-2010, 11:28 PM
  4. Purchase orders 2 in 1
    By jordanturner in forum Forms
    Replies: 1
    Last Post: 09-09-2010, 03:38 AM
  5. Replies: 13
    Last Post: 05-28-2010, 11: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