Queries - Sum of Calculated Fields

PrinceHairy

New Member
Joined
Apr 28, 2015
Messages
10
Hi all,

I have the following databases (a sample) and am trying to obtain a summary query from these:

Sales:
DateProduct
Colour
Qty. Sold
16/03/14
Ball
Yellow
4
17/03/14
Racket
White
3
19/03/14
Racket
Blue
7
20/03/14
Ball
Red
5
20/03/14
Racket
White
1
22/03/14
Ball
Red
1

<tbody>
</tbody>

Inventory:
Product
Colour
SOH as at end of trade 31/12/2013
Ball
Yellow
7
Ball
Red
6
Racket
White
4
Racket
Blue
8

<tbody>
</tbody>

This is what my query should look like:
Product
SOH at end of 2013
Quantity Sold
Current SOH
Ball
13
10
3
Racket
12
11
1

<tbody>
</tbody>

However, I suspect because I'm trying to obtain the sum of some calculated fields, it's outputting some astronomical values:
Product
SOH at end of 2013
Quantity Sold
Current SOH
Ball
78
42
36
Racket
72
42
30

<tbody>
</tbody>


I suspect the way to fix this would be via using a SQL query instead of the query design - any ideas?
 
Try this:

Code:
SELECT X.ProdID, Sum(X.SOH31122013) AS BI, ((select sum(QtySold) from Sales where ProdID = x.ProdID)) AS QS, [BI]-[QS] AS CurrentInv
FROM Inventory AS X
GROUP BY X.ProdID;
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try this:

Code:
SELECT X.ProdID, Sum(X.SOH31122013) AS BI, ((select sum(QtySold) from Sales where ProdID = x.ProdID)) AS QS, [BI]-[QS] AS CurrentInv
FROM Inventory AS X
GROUP BY X.ProdID;

The only thing is I would still like the output to be like below (by Product name and not colour-specific), hence why I tried keeping the X.ProductName to no avail.

Product
BI
QS
CurrentInv
Ball13103
Racket12111

<tbody>
</tbody>
 
Upvote 0
I posted this problem on UtterAccess and azolder replied with:

Code:
SELECT Products.Product, Sum(Inventory.SOH31122013) AS Inventory, Sum(qryTotSold.TS) AS QtySold, Sum([SOH31122013]-[ts]) AS Difference
FROM (Products INNER JOIN Inventory ON Products.ProdID = Inventory.ProdID) INNER JOIN [SELECT Sales.ProdID, Sum(Sales.QtySold) AS TS FROM Sales GROUP BY Sales.ProdID ]. AS qryTotSold ON Products.ProdID = qryTotSold.ProdID
GROUP BY Products.Product;

it worked on the sample
 
Upvote 0
Oh wow, looking at that is mind-boggling itself haha. Managed to get it to work with my database - thanks a lot for that and all your help! :)
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,822
Members
449,470
Latest member
Subhash Chand

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