asonofTheAlmighty
New member
- Local time
- Today, 13:01
- Joined
- May 30, 2014
- Messages
- 3
I am trying to design a database to keep records for a literature distribution organization. I have the basic design figured out patterned after the Northwind database wherever applicable. I am now trying to create reports to track Purchases, Sales, AND PROFIT by Customer and/or by Product for any chosen time period. I have a query called "Closed Purchases" with fields of PurchaseID, PurchaseDetailsID, Product, Qty, ActualCost, and ArrivalDate. (These are captions not the actual fields names.) I also have a query called "Closed Sales" with fields of SalesID, SalesDetailsID, Product (actual field is od_ProductID which is stored as a number but pulls the Product Title from the Products table), Qty, Selling Price, and OrderDate. Based on the assumption that oldest inventory is automatically sold first, I would like to know the Profit for every item sold based on the ActualCost paid for THAT PARTICULAR item.
How I did this was to create a new query called "Closed Sales Individualized" that broke all the sales down into individual items. In other words, what had been a Product of "BookTitle1" with a Qty of 40 would now be 40 individual records of "BookTitle1" numbered 1 through 40.
I then made a new query called "Closed Sales Individualized Sorted a-z" that pulled all the fields from the last one and added a new ID field by concatenating the Product, OrderDate, SalesID, SalesDetailsID, and Rank fields. I then sorted by this new field. So now it looked something like: 40 records of "BookTitle1" numbered 1-40; 32 records of "BookTitle1" (but of a newer Date) numbered 1-32; 25 records of "BookTitle2" numbered 1-25; 24 more records of "BookTitle2" (with a newer Date) numbered 1-24; etc.
Next, I made a new query called "Sales Ranked" that pulled just the Product and UniqueID fields from the last query. To this one I added a field called Rank (that included a subquery with Count function) that numbered each Product as a group: so now it was 40 records of "BookTitle1" of "Date1" followed by 32 records of "BookTitle1" of "Date2" now numbered 1-72 (instead of 1-40 and 1-32).... So all items of a Product are now grouped together and ordered by the order in which they were sold.
I then made a string of similar queries for "Closed Purchases". Next, I took the "Sales Ranked" query and the "Purchases Ranked" query and combined them into a query called "Sales matched to Purchases" that joined the two queries wherever the Product and Rank were identical. From this I made a new query called "Sales/Purchases Summary" that used the ID from the sales side of the "Sales matched to Purchases" query to pull the rest of the details from "Closed Sales Individualized Sorted a-z" and the ID from the purchases side to pull the wanted details from "Closed Purchases Individualized Sorted a-z". To this query I added a field to calculate the Profit. I now have a query that shows every Sale with its Selling Price and with its Actual Cost (and Profit) based on the order of sale compared to the order of purchase.
The only problem: it runs very slowly! The last query takes more than a full minute to run even though there are only 262 individual sold items. The bottleneck is in the "Sales Ranked" and "Purchases Ranked" queries. The function that is used to rank the records is very intensive and slow. Here is the SQL of the "Sales Ranked" query:
SELECT [Closed Sales Individualized Sorted a-z].ID, a.od_ProductID, IIf([a]![od_ProductID]=[Closed Sales Individualized Sorted a-z]![od_ProductID],(SELECT COUNT(ID) FROM [Closed Sales Individualized Sorted a-z] WHERE (ID < a!ID) and (a!od_ProductID = od_ProductID)),"")+1 AS RANK
FROM [Closed Sales Individualized Sorted a-z] AS a INNER JOIN [Closed Sales Individualized Sorted a-z] ON a.ID = [Closed Sales Individualized Sorted a-z].ID
ORDER BY [Closed Sales Individualized Sorted a-z].ID;
So, someone surely has a better idea how to accomplish what I am trying to do. Is there a better way to rank the records so that they can be joined. Or is there a common way of accomplishing this type of report completely different from the way I set out to do it. After all, I expect that there are many companies that keep track of the actual cost paid for each of the 12 copies of Item1 that they sold today even when 5 of those copies were part of a purchase 13 months ago and the other 7 part of a purchase (of a higher price) only 8 months ago.
I hope you can make sense of this. If not, please ask for clarification. (I understand very little of VBA coding and a very limited amount of SQL. I prefer to work in Design View and only switch to SQL to make final adjustments that Design View cannot do. So hopefully there is a way to do this with my limited understanding. ???) Thank you very much for your help. I have been working on this Report alone for more that two days.
How I did this was to create a new query called "Closed Sales Individualized" that broke all the sales down into individual items. In other words, what had been a Product of "BookTitle1" with a Qty of 40 would now be 40 individual records of "BookTitle1" numbered 1 through 40.
I then made a new query called "Closed Sales Individualized Sorted a-z" that pulled all the fields from the last one and added a new ID field by concatenating the Product, OrderDate, SalesID, SalesDetailsID, and Rank fields. I then sorted by this new field. So now it looked something like: 40 records of "BookTitle1" numbered 1-40; 32 records of "BookTitle1" (but of a newer Date) numbered 1-32; 25 records of "BookTitle2" numbered 1-25; 24 more records of "BookTitle2" (with a newer Date) numbered 1-24; etc.
Next, I made a new query called "Sales Ranked" that pulled just the Product and UniqueID fields from the last query. To this one I added a field called Rank (that included a subquery with Count function) that numbered each Product as a group: so now it was 40 records of "BookTitle1" of "Date1" followed by 32 records of "BookTitle1" of "Date2" now numbered 1-72 (instead of 1-40 and 1-32).... So all items of a Product are now grouped together and ordered by the order in which they were sold.
I then made a string of similar queries for "Closed Purchases". Next, I took the "Sales Ranked" query and the "Purchases Ranked" query and combined them into a query called "Sales matched to Purchases" that joined the two queries wherever the Product and Rank were identical. From this I made a new query called "Sales/Purchases Summary" that used the ID from the sales side of the "Sales matched to Purchases" query to pull the rest of the details from "Closed Sales Individualized Sorted a-z" and the ID from the purchases side to pull the wanted details from "Closed Purchases Individualized Sorted a-z". To this query I added a field to calculate the Profit. I now have a query that shows every Sale with its Selling Price and with its Actual Cost (and Profit) based on the order of sale compared to the order of purchase.
The only problem: it runs very slowly! The last query takes more than a full minute to run even though there are only 262 individual sold items. The bottleneck is in the "Sales Ranked" and "Purchases Ranked" queries. The function that is used to rank the records is very intensive and slow. Here is the SQL of the "Sales Ranked" query:
SELECT [Closed Sales Individualized Sorted a-z].ID, a.od_ProductID, IIf([a]![od_ProductID]=[Closed Sales Individualized Sorted a-z]![od_ProductID],(SELECT COUNT(ID) FROM [Closed Sales Individualized Sorted a-z] WHERE (ID < a!ID) and (a!od_ProductID = od_ProductID)),"")+1 AS RANK
FROM [Closed Sales Individualized Sorted a-z] AS a INNER JOIN [Closed Sales Individualized Sorted a-z] ON a.ID = [Closed Sales Individualized Sorted a-z].ID
ORDER BY [Closed Sales Individualized Sorted a-z].ID;
So, someone surely has a better idea how to accomplish what I am trying to do. Is there a better way to rank the records so that they can be joined. Or is there a common way of accomplishing this type of report completely different from the way I set out to do it. After all, I expect that there are many companies that keep track of the actual cost paid for each of the 12 copies of Item1 that they sold today even when 5 of those copies were part of a purchase 13 months ago and the other 7 part of a purchase (of a higher price) only 8 months ago.
I hope you can make sense of this. If not, please ask for clarification. (I understand very little of VBA coding and a very limited amount of SQL. I prefer to work in Design View and only switch to SQL to make final adjustments that Design View cannot do. So hopefully there is a way to do this with my limited understanding. ???) Thank you very much for your help. I have been working on this Report alone for more that two days.