Getting Sum() Of Second Table

Dave Punky

Board Regular
Joined
Jan 7, 2010
Messages
133
Hi all,

I'm trying to do something that I think should be simple but seems to be presenting more issues than I anticipated.

Basically I have two tables, which look a bit like so:

Table A

IDTitleTypeTable
1AA1Table A
2BA2Table A

<tbody>
</tbody>

Table B

IDSearched IDResult
1110
2110
3210

<tbody>
</tbody>

I've been trying to write a query that sums the results of Table B and groups by the Searched ID basically creates a recordset which would look like so with the information above:

IDTitleTypeTableResult
1AA1Table A20
2BA2Table A10

<tbody>
</tbody>


My current SQL query is like so:

Code:
SELECT [ID], [Title], [Type],  [Table], (SELECT Sum([Table B.Result]) AS sum FROM Table B WHERE [Table A.ID]=[Table B.Searched ID]) AS [Result] 
FROM Table A 
WHERE [Title] LIKE "*A*" 
ORDER BY [Result] DESC, [Title] ASC;

I've got this working over a union query for multiple tables, however when trying to just look at these two tables Access is expecting an operator for [Result] that I can't seem to account for. I'm actually sending the query via VBA from Excel so it just errors advising it doesn't have the minimum parameters.

I'm almost certain I'm doing something obviously wrong but I can't seem to quite work it out - any advice on what I've done wrong in my query above? I've tried doing a left join as well from Table A to B but I'm having issues grouping the ID's together in Table B to subsequently attach them to the initial table.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try this instead:

SELECT [Table B].[Searched ID], [Table A].Title, [Table A].Type, [Table A].Table, Sum([Table B].Result) AS SumOfResult
FROM [Table A] INNER JOIN [Table B] ON [Table A].ID = [Table B].[Searched ID]
GROUP BY [Table B].[Searched ID], [Table A].Title, [Table A].Type, [Table A].Table;

This is what my results look like:

Searched IDTitleTypeTableSumOfResult
1AA1Table A20
2BA2Table A10

<caption> Query1 </caption><thead>
</thead><tbody>
</tbody><tfoot></tfoot>
 
Last edited:
Upvote 0
Hi Alan,

First of all that solution has worked for me after adding some WHERE criterial, so thank you very much for that.

However I'm trying to do an ORDER BY the SumOfResult and I'm encountering the same issues as before where Access expects a value for SumOfResult (and therefore I get a too few parameters met error via VBA).

To put into perspective I am just trying to add the following to the end of your statement:
Code:
ORDER BY [SumOfResult] DESC, [Title] ASC;

I feel like I'm missing something obvious but I can't quite put my finger on it as this would just work with a regular statement.
 
Upvote 0
Instead of:
ORDER BY [SumOfResult] DESC, [Title] ASC;

Try:
Code:
ORDER BY Sum([Table B].Result), [Title] ASC;


Usually you cannot assume SQL knows what an alias in the SELECT clause means.


------------------------------------


Note that you *could* use the column alias if you nest the query in a subquery and then "query the query":

Code:
SELECT
	T.[Searched ID],
	T.Title,
	T.[Type],
	T.[Table],
	T.SumOfResult 
FROM
(
	SELECT 
		[Table B].[Searched ID], 
		[Table A].Title,
		[Table A].[Type], 
		[Table A].[Table], 
		Sum([Table B].Result) AS SumOfResult
	FROM 
		[Table A] 
		INNER JOIN 
		[Table B] ON [Table A].ID = [Table B].[Searched ID]
	GROUP BY 
		[Table B].[Searched ID], 
		[Table A].Title, 
		[Table A].[Type], 
		[Table A].[Table]
) AS T

ORDER BY T.SumOfResult
 
Last edited:
Upvote 0
Thanks so much xenou that's exactly what I needed, and thanks for explaining so coherently as well. I knew it must have been something obvious but I never would have guessed that!
 
Upvote 0
Ok I've encountered another issue with this query - in the event there are no results in Table B, I still need it to display the results of Table A (but with a value of 0 for the sum). At the moment it's not displaying the results (because its displaying the ID information from Table B rather than Table A).

I did the obvious thing of changing the ID reference in the table from Table B to Table A but its still not including results which would have a null result from Table B. I've tried adding in the following to the sum but it's made no difference...
Code:
...[Table A.Table], IIF(ISNULL(Sum([Table B.Result]),0,Sum([Table B.Result])) AS SumOfResult
 
Upvote 0
Hi,
Change INNER JOIN to LEFT JOIN. That would be the usual solution. Let me know if it doesn't work (if not, post your complete query as it stands now).

With a LEFT JOIN, all fields from the table on the left side are returned, with matched fields from the table on the right side, if there are any. If the table on the right side doesn't have a match, you get Nulls instead. Note that Sum will ignore Nulls, so it shouldn't affect the outcome (you can think of the missing records as being zeros). By left and right side, we just mean that A LEFT JOIN B has Table A on the left and Table B on the right; so all of A's records are returned with any matching B records.
 
Upvote 0

Forum statistics

Threads
1,214,797
Messages
6,121,629
Members
449,041
Latest member
Postman24

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