Subtotal Query Results

student

Registered User.
Local time
Today, 22:07
Joined
Oct 3, 2011
Messages
45
I'm trying to create an easy Excel sheet for a few departments at work showing production of certain items in the factory, split by product dept.

I have the queries written in Access and am using MS Query to bring the (filtered) results through to Excel. How I have it currently is the different product depts on different sheets, but would like to subtotal production of each sub-dept as well. For example, all dept 40 products on one sheet, but subtotals required for 40.1, 40.2, 40.3 sub-groups to show on results alongside grand totals as well. Can be done manually, but I want a refreshable report that I can just send out once.

Any guidance would be appreciated as the guys here don't know how to do it either.
 
Instead of pulling the data from Access using Excel, you could push the data from Access and use automation to insert rows and the total calculations. Another idea is to create a second query to calculate the totals, use a UNION of the original query and the totals, and pull the UNION query into Excel. If you will post the SQL of your query, I can give you an idea how to do that.
 
Sounds fantastic. If you could give some clues it would be great as I'm still on a very steep learning curve atm!
Original query in Access is:
Code:
[COLOR=black][FONT=Verdana]SELECT [1-1 history week].dated AS movement_date, [1-3 sales week].dated AS sales_date, [1-0 products].dept, [Manufacturing Department].[Department Name], [Sub Group].prod_type, [1-0 products].product, [1-0 products].long_description, [1-0 products].sub_grp, [1-2 bom].alpha_code, [1-0 products].unit_code, [1-0 products].ship_cat AS [sheets/box], [1-1 history week].SumOfmovement_quantity AS produced, [1-0 products]![ship_cat]*[1-1 history week]![SumOfmovement_quantity] AS sheets, [1-3 sales week].SumOfquantity AS sales_qty, [1-0 products]![ship_cat]*[1-3 sales week]![SumOfquantity] AS sheets_sold, [1-3 sales week].SumOfval AS sales_val, [1-3 sales week].SumOfcost AS cost, [1-2 bom].instructions1, [1-2 bom].instructions2, [1-2 bom].instructions3, [1-2 bom].instructions4[/FONT][/COLOR]
[FONT=Verdana][COLOR=black]FROM (((([1-0 products] LEFT JOIN [1-2 bom] ON [1-0 products].product = [1-2 bom].product_code) LEFT JOIN [1-1 history week] ON [1-0 products].product = [1-1 history week].product) LEFT JOIN [1-3 sales week] ON [1-0 products].product = [1-3 sales week].product) INNER JOIN [Manufacturing Department] ON [1-0 products].dept = [Manufacturing Department].[Department No]) INNER JOIN [Sub Group] ON [1-0 products].sub_grp = [Sub Group].[sub grp][/COLOR][/FONT]
[FONT=Verdana][COLOR=black]WHERE ((([1-0 products].dept)="40") AND (([1-1 history week].SumOfmovement_quantity) Is Not Null)) OR ((([1-0 products].dept)="40") AND (([1-3 sales week].SumOfquantity) Is Not Null));[/COLOR][/FONT]
Where "[1-0 products].dept" denotes on what sheet the results should display (filtered here but if SQL can run them all together and split onto different sheets then that's great! I need subtotals of these by “[1-0 products].sub_grp”:
[1-1 history week].SumOfmovement_quantity AS produced
[1-0 products]![ship_cat]*[1-1 history week]![SumOfmovement_quantity] AS sheets
[1-3 sales week].SumOfquantity AS sales_qty
[1-0 products]![ship_cat]*[1-3 sales week]![SumOfquantity] AS sheets_sold
[1-3 sales week].SumOfval AS sales_val
[1-3 sales week].SumOfcost AS cost

As I said, the issue is that I currently have no idea how to achieve this, so you’re help/guidance would be massively appreciated! Hope this makes sense.
 
Last edited:
OK, let's call the query below "qryDetails". A "totals" query to get the values you want would look like:

Code:
SELECT qryDetails.long_description, qryDetails.sub_grp, Sum(qryDetails.produced) As TotalProduced, 
Sum(qryDetails.sheets) As TotalSheets, Sum(qryDetails.sales_qty) As TotalSales_Qty, 
Sum(qryDetails.sheets_sold) As TotalSheets_Sold, Sum(qryDetails.sales_val) As TotalSales_Val, 
Sum(Cost) As TotalCost
FROM qryDetails
GROUP BY qryDetails.long_description, qryDetails.sub_grp;

You don't have an Order By clause in your original SQL, so it's tough to figure out what the sequence of the rows returned by Access will be. To do a "UNION" of the two queries, you'll need to add "dummy" columns to match up with the columns other than long_description, sub_grp, and the 6 Sum fields, then sort it so that the "sum" row comes out at the bottom of each group. How are you sorting the data?


Sounds fantastic. If you could give some clues it would be great as I'm still on a very steep learning curve atm!
Original query in Access is:
Code:
[COLOR=black][FONT=Verdana]SELECT [1-1 history week].dated AS movement_date, [1-3 sales week].dated AS sales_date, [1-0 products].dept, [Manufacturing Department].[Department Name], [Sub Group].prod_type, [1-0 products].product, [1-0 products].long_description, [1-0 products].sub_grp, [1-2 bom].alpha_code, [1-0 products].unit_code, [1-0 products].ship_cat AS [sheets/box], [1-1 history week].SumOfmovement_quantity AS produced, [1-0 products]![ship_cat]*[1-1 history week]![SumOfmovement_quantity] AS sheets, [1-3 sales week].SumOfquantity AS sales_qty, [1-0 products]![ship_cat]*[1-3 sales week]![SumOfquantity] AS sheets_sold, [1-3 sales week].SumOfval AS sales_val, [1-3 sales week].SumOfcost AS cost, [1-2 bom].instructions1, [1-2 bom].instructions2, [1-2 bom].instructions3, [1-2 bom].instructions4[/FONT][/COLOR]
[FONT=Verdana][COLOR=black]FROM (((([1-0 products] LEFT JOIN [1-2 bom] ON [1-0 products].product = [1-2 bom].product_code) LEFT JOIN [1-1 history week] ON [1-0 products].product = [1-1 history week].product) LEFT JOIN [1-3 sales week] ON [1-0 products].product = [1-3 sales week].product) INNER JOIN [Manufacturing Department] ON [1-0 products].dept = [Manufacturing Department].[Department No]) INNER JOIN [Sub Group] ON [1-0 products].sub_grp = [Sub Group].[sub grp][/COLOR][/FONT]
[FONT=Verdana][COLOR=black]WHERE ((([1-0 products].dept)="40") AND (([1-1 history week].SumOfmovement_quantity) Is Not Null)) OR ((([1-0 products].dept)="40") AND (([1-3 sales week].SumOfquantity) Is Not Null));[/COLOR][/FONT]
Where "[1-0 products].dept" denotes on what sheet the results should display (filtered here but if SQL can run them all together and split onto different sheets then that's great! I need subtotals of these by “[1-0 products].sub_grp”:
[1-1 history week].SumOfmovement_quantity AS produced
[1-0 products]![ship_cat]*[1-1 history week]![SumOfmovement_quantity] AS sheets
[1-3 sales week].SumOfquantity AS sales_qty
[1-0 products]![ship_cat]*[1-3 sales week]![SumOfquantity] AS sheets_sold
[1-3 sales week].SumOfval AS sales_val
[1-3 sales week].SumOfcost AS cost

As I said, the issue is that I currently have no idea how to achieve this, so you’re help/guidance would be massively appreciated! Hope this makes sense.
 
Data not currently sorted for output, but natural sort would be by [Manufacturing Department].[Department Name], then [Sub Group].prod_type, then [1-0 products].long_description, all ASC. Clearly, if being filtered to one dept per sheet, the first sort order here would be omitted.

You're thinking a 'line-type' number to distinguish between result line & subtotal/total line?
 
Note that my Totals query as written will sort on long_description, then sub_grp, not prod_type. I think I would need to replace sub_grp with prod_type to get something to work with. Is that really how you want it grouped? It should match your sort sequence.

Can you give me the data type of all the columns output by your main query? With that, I think I can construct the UNION query that will solve your problem. (Provided you also answer my question above.)
 
Sorry - failed to clarify that one. sub_grp is the numerical representation of prod_type, that's all. long_description is name of product, dept is how the results need split onto different sheets, then sub-divided by sub_grp/prod_type.


Data-types of output are listed in the attached image.
 

Attachments

  • Capture.PNG
    Capture.PNG
    16.8 KB · Views: 249
OK, just for review. Your query that should be named qryDetails:

SELECT [1-1 history week].dated AS movement_date, [1-3 sales week].dated AS sales_date, [1-0 products].dept,
[Manufacturing Department].[Department Name], [Sub Group].prod_type, [1-0 products].product, [1-0 products].long_description,
[1-0 products].sub_grp, [1-2 bom].alpha_code, [1-0 products].unit_code, [1-0 products].ship_cat AS [sheets/box],
[1-1 history week].SumOfmovement_quantity AS produced,
[1-0 products]![ship_cat]*[1-1 history week]![SumOfmovement_quantity] AS sheets, [1-3 sales week].SumOfquantity AS sales_qty,
[1-0 products]![ship_cat]*[1-3 sales week]![SumOfquantity] AS sheets_sold, [1-3 sales week].SumOfval AS sales_val,
[1-3 sales week].SumOfcost AS cost, [1-2 bom].instructions1, [1-2 bom].instructions2, [1-2 bom].instructions3,
[1-2 bom].instructions4
FROM (((([1-0 products] LEFT JOIN [1-2 bom]
ON [1-0 products].product = [1-2 bom].product_code)
LEFT JOIN [1-1 history week]
ON [1-0 products].product = [1-1 history week].product)
LEFT JOIN [1-3 sales week]
ON [1-0 products].product = [1-3 sales week].product)
INNER JOIN [Manufacturing Department]
ON [1-0 products].dept = [Manufacturing Department].[Department No]) INNER JOIN [Sub Group]
ON [1-0 products].sub_grp = [Sub Group].[sub grp]
WHERE ((([1-0 products].dept)="40") AND
(([1-1 history week].SumOfmovement_quantity) Is Not Null)) OR
((([1-0 products].dept)="40") AND
(([1-3 sales week].SumOfquantity) Is Not Null));

My query that should be named qryTotals:
SELECT qryDetails.prod_type, qryDetails.long_description, Sum(qryDetails.produced) As TotalProduced,
Sum(qryDetails.sheets) As TotalSheets, Sum(qryDetails.sales_qty) As TotalSales_Qty,
Sum(qryDetails.sheets_sold) As TotalSheets_Sold, Sum(qryDetails.sales_val) As TotalSales_Val,
Sum(Cost) As TotalCost
FROM qryDetails
GROUP BY qryDetails.prod_type, qryDetails.long_description;

UNION query:
SELECT qryDetails.*, "" As Seq
FROM qryDetails
UNION ALL
SELECT Null As movement_Date, Null As sales_Date, "" As dept, "" As [Department Name], qryTotals.prod_type, "" As product,
qryTotals.long_description, "" As sub_grp, "" As alpha_code, "" As unit_code, "" As [sheets/box], qryTotals.TotalProduced, qryTotals.TotalSheets, qryTotals.TotalSales_Qty, qryTotals.TotalSheets_Sold,
qryTotals.TotalSales_Val, qryTotals.TotalCost, "" As instructions1, "" As instructions2, "" As instructions3, "" As instructions4, "Totals" As Seq
FROM qryTotals
ORDER BY prod_type, long_description, Seq;

That should give you all the details rows for a prod_type and long_description followed by the one totals row. You can then use that to export to Excel from Access or import to Excel from Excel.


John Viescas, author
Microsoft Office Access 2010 Inside Out
Microsoft Office Access 2007 Inside Out
Building Microsoft Access Applications
Microsoft Office Access 2003 Inside Out
SQL Queries for Mere Mortals
http://www.viescas.com/
(Paris, France)
 
Absolutely fantastic and very impressive, thank you! After a couple of teething issues with timeouts that seems to be working brilliantly. One issue I'm having now though is that it provides subtotals by long_description, not prod_type. I played with the code a little in the vain hope this would help but it gives the same subtotals, just all together by prod_type but doesn't combine the subtotals into one for each prod_type as required. Any chance you could help out with this issue as well? I'd want a subtotal for each prod_type, not for each long_description.
 
Sure. Change qryTotals to this:

SELECT qryDetails.prod_type, Sum(qryDetails.produced) As TotalProduced,
Sum(qryDetails.sheets) As TotalSheets, Sum(qryDetails.sales_qty) As TotalSales_Qty,
Sum(qryDetails.sheets_sold) As TotalSheets_Sold, Sum(qryDetails.sales_val) As TotalSales_Val,
Sum(Cost) As TotalCost
FROM qryDetails
GROUP BY qryDetails.prod_type;

.. and change the UNION query to this:

SELECT qryDetails.*, "" As Seq
FROM qryDetails
UNION ALL
SELECT Null As movement_Date, Null As sales_Date, "" As dept, "" As [Department Name], qryTotals.prod_type, "" As product, "" As long_description,
"" As sub_grp, "" As alpha_code, "" As unit_code, "" As [sheets/box], qryTotals.TotalProduced, qryTotals.TotalSheets, qryTotals.TotalSales_Qty, qryTotals.TotalSheets_Sold,
qryTotals.TotalSales_Val, qryTotals.TotalCost, "" As instructions1, "" As instructions2, "" As instructions3, "" As instructions4, "Totals" As Seq
FROM qryTotals
ORDER BY prod_type Seq;
 
You're an absolute legend, thank you. Just working on best outputs now that it's all working.

Cheers again.
 

Users who are viewing this thread

Back
Top Bottom