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?
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Are you using an inner or cross join? Both can be done in design view. A quick guaranteed fix is the make-table query (analogous to paste-values in Excel), but you should be able to get the proper sums without one.
 
Upvote 0
Are you using an inner or cross join? Both can be done in design view. A quick guaranteed fix is the make-table query (analogous to paste-values in Excel), but you should be able to get the proper sums without one.

I'm actually not using any join at the moment. Can you run me through the steps for that? It's an area I don't know a lot about.

I did manage to fix it via the make-table query method, but because I'm dealing with a big database, I'm looking to make the database as small a filesize as possible.
 
Upvote 0
If Sales and Inventory are separate tables then you would list them both via Show Table in the query design view, then drag the Product field name from one into the other. A line connecting them will be displayed, and INNER JOIN will be visible in the SQL view window. Then when you add fields only the records common to both tables will appear, corresponding to each product. Use a summary query of the Sales table of course (without the Date field) so you get distinct rows.
 
Upvote 0
Awesome! That did the trick! Do you know of a way to perform this without having to use a summary query? Just trying to minimise the filesize.
 
Upvote 0
You can write the join and distinctrow in one query, but I don't think a subquery, Dsum, report grouping, or any other method is more efficient.
 
Upvote 0
You can write the join and distinctrow in one query, but I don't think a subquery, Dsum, report grouping, or any other method is more efficient.

I had a go, but it's not working (comes up with Syntax error). Any ideas?
Code:
SELECT Sales.Product,
           [Inventory.SOH as at end 2013],
           Sales.QtySold,
FROM Sales
           JOIN (SELECT DISTINCT Product, [SOH as at end of 2013] from Inventory) Inventory ON Sales.Product = ProductName
GROUP BY Sales.Product;
 
Upvote 0
Yeah it's trickier than I thought, if even possible. Here's the subquery:

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

(I changed two field names in the tables: SOHDec312013 and QtySold because of the syntax with the spaces and the underscores didn't seem to work for some reason)
 
Upvote 0
Yeah it's trickier than I thought, if even possible. Here's the subquery:

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

(I changed two field names in the tables: SOHDec312013 and QtySold because of the syntax with the spaces and the underscores didn't seem to work for some reason)

That worked perfectly! Just a quick question - how would I do it if there is a Product ID for each product? (I'm planning on normalising the database that I have)

Products:
ProdID
Product
Colour
B0001BallYellow
R0001RacketWhite
R0002RacketBlue
B0002BallRed

<tbody>
</tbody>

Sales:
DateProdID
QtySold
16/03/14B00014
17/03/14R00013
19/03/14R00027
20/03/14B00025
20/03/14R00011
22/03/14B00021

<tbody>
</tbody>


Inventory:
ProdID
SOH31122013
B00017
B00026
R00014
R00028

<tbody>
</tbody>
 
Upvote 0
I've tried the following to no avail:
Code:
SELECT X.Product, ((select sum(SOH31122013) from Inventory where ProdID = Products.ProdID)) AS BI, Sum(X.QtySold) AS SumOfQtySold, [BI]-[SumOfQtySold] AS CurrentInv
FROM Sales AS X
GROUP BY X.Product;
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,876
Members
449,056
Latest member
ruhulaminappu

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