Having Subquery Trouble

sumnicme

New Member
Joined
Sep 25, 2018
Messages
4
I have the following query:
Item NoItem ClassDept CodeQuantity
0001Raw Material150
0001Raw Material230
0002Raw Material3100
0003Raw Material175
0003Raw Material2100
0003Raw Material320
0004WIP Item4100
0005WIP Item4100
0005WIP Item5175

<tbody>
</tbody>


I want to add another column showing what percentage of each item's total consumption is related to the department:

Item NoItem ClassDept CodeQuantityItem Total% of Quantity
0001Raw Material1508062.5%
0001Raw Material2308037.5%
0002Raw Material390
90100%
0003Raw Material17519538.5%
0003Raw Material210019551.3%
0003Raw Material32019510.3%
0004WIP Item4100100100%
0005WIP Item410027536.4%
0005WIP Item517527565.6%

<tbody>
</tbody>

I've tried the below text, but I get a syntax error

Item Total: (Select Sum(Quantity)
FROM [Consumption by Item & Dept] AS [Total Consumption by Item]
WHERE [Total Consumption by Item].[Item No] = [Consumption by Item & Dept].[Item No])

I am very new to access and have tried to correct this a few ways, but don't know what I'm doing wrong.

Thanks!
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
I have the following query:
Item No Item Class Dept Code Quantity
0001 Raw Material 1 50
0001 Raw Material 2 30
0002 Raw Material 3 100
0003 Raw Material 1 75
0003 Raw Material 2 100
0003 Raw Material 3 20
0004 WIP Item 4 100
0005 WIP Item 4 100
0005 WIP Item 5 175

An independent second query would be useful:
-------------------------------------
| Item No | Item Class   | Quantity |
-------------------------------------
|       1 | Raw Material |       80 |
|       2 | Raw Material |      100 |
|       3 | Raw Material |      195 |
|       4 | WIP Item     |      100 |
|       5 | WIP Item     |      275 |
-------------------------------------


using those two queries together with a simple join statement would get you to the results you want I think. This would be an alternative to using subqueries (probably there is more than one possible solution to this one).
 
Upvote 0
I've tried using an independent query and it pulls in the total number, but when it comes time to calculate the percentage dividing column 4 by column 5, it asks me to enter a parameter value and I can't figure out why.
 
Upvote 0
SELECT [ILE - Consumption].[Item No], [Item List].[Item Type], [Document Dept].[Dept Code], Sum([ILE - Consumption].Quantity) AS SumOfQuantity, Sum([ILE - Consumption].[Cost Amount (Actual)]) AS [SumOfCost Amount (Actual)], [Consumption by Item].[Sum Of Quantity], [SumOfQuantity]/[Sum Of Quantity] AS Expr1
FROM ([Document Dept] INNER JOIN ([Item List] INNER JOIN [ILE - Consumption] ON [Item List].[Item No] = [ILE - Consumption].[Item No]) ON [Document Dept].[No] = [ILE - Consumption].[Document No]) INNER JOIN [Consumption by Item] ON [Item List].[Item No] = [Consumption by Item].[Item No]
GROUP BY [ILE - Consumption].[Item No], [Item List].[Item Type], [Document Dept].[Dept Code], [Consumption by Item].[Sum Of Quantity], [SumOfQuantity]/[Sum Of Quantity];
 
Upvote 0
This was not my suggestion. For starters, you have four tables here not two. You also haven't worked out the sum in an independent query (a single query all by itself that would give you three columns - Item No, Item Class, and Quantity.
 
Upvote 0
To reiterate, the suggestion was to use two queries. One was your first query in your first post. The other is the one I suggested you create in my first post.

Then you simply join them (ignoring for now the inevitable division by zero errors and also whether or not you need to be careful about integer vs. floats in your quantity values).

Code:
/* 
COMMENT
    Query1: returns ItemNo, ItemClass, DeptCode, Quantity 
    Query2: returns ItemNo, ItemClass, ItemTotalQuantity
END COMMENT
*/

select 
	Query1.ItemNo, 
	Query1.ItemClass, 
	Query1.DeptCode, 
	Query1.Quantity, 
	Query2.ItemTotalQuantity, 
	(Query1.Quantity/Query2.ItemTotalQuantity) as PCTOfQuantity
from
	Query1
	inner join Query2
	on Query1.ItemNo = Query2.ItemNo
	and Query1.ItemClass = Query2.ItemClass

Don't use the comments in MSAccess and adjust columns names as needed - columns with spaces or special characters must be enclosed in [brackets]
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top