Results 1 to 5 of 5
  1. #1
    ManuelLavesa is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Sep 2015
    Posts
    113

    Finding Product Sales for the last 3 years

    Hello, This time I am trying to write a query for finding out the product sales for the current year + 3 years previously (e.i. 2014,2013,2012) in order to see the trend of the product sales. to do this I started a query which has the following SQL:



    SubQryTotalProductSalesFor3Years

    SELECT Product.ProductName, Sum(OrderDetails.QtyOrdered) AS SumOfQtyOrdered, Year(Date()) AS CurrentYear, [CurrentYear]-1 AS LastYear, [CurrentYear]-2 AS 2YearsAgo, [CurrentYear]-3 AS 3YearsAgo
    FROM Product INNER JOIN (Orders INNER JOIN OrderDetails ON Orders.OrderID = OrderDetails.fkOrderID) ON Product.ProductID = OrderDetails.fkProductID
    GROUP BY Product.ProductName, Year(Date()), [CurrentYear]-1, [CurrentYear]-2, [CurrentYear]-3
    ORDER BY Product.ProductName;

    This query works but always pops a message box asking for the "CurrentYear" value, if I leave it blank and click ok the result is 2015 and does what is expected. Then I am using this query as a subquery in another query named Qry3yrsProductSales and it has the following SQL:

    SELECT SubQryTotalProductSalesFor3Years.ProductName, SubQryTotalProductSalesFor3Years.SumOfQtyOrdered AS Quantity, SubQryTotalProductSalesFor3Years_1.SumOfQtyOrdered AS QtyYr1, SubQryTotalProductSalesFor3Years_2.SumOfQtyOrdered AS QtyYr2, SubQryTotalProductSalesFor3Years_3.SumOfQtyOrdered AS QtyYr3
    FROM ((SubQryTotalProductSalesFor3Years LEFT JOIN SubQryTotalProductSalesFor3Years AS SubQryTotalProductSalesFor3Years_1 ON (SubQryTotalProductSalesFor3Years.LastYear = SubQryTotalProductSalesFor3Years_1.CurrentYear) AND (SubQryTotalProductSalesFor3Years.ProductName = SubQryTotalProductSalesFor3Years_1.ProductName)) LEFT JOIN SubQryTotalProductSalesFor3Years AS SubQryTotalProductSalesFor3Years_2 ON (SubQryTotalProductSalesFor3Years.ProductName = SubQryTotalProductSalesFor3Years_2.ProductName) AND (SubQryTotalProductSalesFor3Years.[2YearsAgo] = SubQryTotalProductSalesFor3Years_2.CurrentYear)) LEFT JOIN SubQryTotalProductSalesFor3Years AS SubQryTotalProductSalesFor3Years_3 ON (SubQryTotalProductSalesFor3Years.ProductName = SubQryTotalProductSalesFor3Years_3.ProductName) AND (SubQryTotalProductSalesFor3Years.[3YearsAgo] = SubQryTotalProductSalesFor3Years_3.CurrentYear);

    This other query also pops the message box asking for the " CurrentYear" value, and then brings only the results for the current Year (2015) but not the other values. evidently I am doing something wrong but I can not figure it out. So if you could help me I will appreciate. Thank you for your help

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Cannot reference calculated field name in its own aggregate query. Apply filter to return only 4 years (current year + 3 previous). What field has the order date?

    SELECT Product.ProductName, Sum(OrderDetails.QtyOrdered) AS SumOfQtyOrdered, Year(OrderDate) AS YrOrder
    FROM Product INNER JOIN (Orders INNER JOIN OrderDetails ON Orders.OrderID = OrderDetails.fkOrderID) ON Product.ProductID = OrderDetails.fkProductID
    GROUP BY Product.ProductName, Year(OrderDate)
    HAVING Year(OrderDate) >= Year(DateAdd("YYYY", -3, Date()))
    ORDER BY Product.ProductName;

    An alternative is to not do aggregate calcs in query. Build report using Sorting & Grouping features and do aggregate calcs in textboxes in group header/footer sections. This 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.

  3. #3
    ManuelLavesa is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Sep 2015
    Posts
    113
    Hello june7 thank you for your reply. You are right that I forgot to put the OrderDate field. Now The OrderDate Field should have been "InvoiceDate" From Table Orders. So in your query I replaced OrderDate with 'InvoicedDate" and for the current year works but not for the other years. As for the report option I agree with you that might be simpler and easier, but I am doing it this way to place the result in a form. Thanks again for your help.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    What do you mean doesn't work for other years? The query should aggregate the data by year and return only the last 4 years. Why a form - the records will not be editable.
    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
    ManuelLavesa is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Sep 2015
    Posts
    113
    Well as always you are right... The query did work is just that I did not noticed the years... Silly me. Well thanks again... And yes I know the records are not editable. But my father in law wants to have it so that he can see all the information about a product in a single Form and then order more or not depending on the info.. I know it sounds crazy but he wants it this way...

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

Similar Threads

  1. Comparing sales for the last 2 years
    By Adele in forum Queries
    Replies: 6
    Last Post: 06-13-2016, 01:34 PM
  2. Building a report comparing customer sales for 3 years
    By controller2850 in forum Reports
    Replies: 1
    Last Post: 03-04-2015, 04:07 PM
  3. Replies: 11
    Last Post: 11-06-2014, 06:01 PM
  4. Comparing Two years of Sales Data
    By Eric@Gopher in forum Programming
    Replies: 2
    Last Post: 08-07-2012, 12:17 PM
  5. Replies: 3
    Last Post: 01-24-2012, 02:44 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