Why do I need to sum two tables before joining them and linking by Group ID to get the Aggregate Total

Galapagos15

Board Regular
Joined
Sep 16, 2015
Messages
100
I have two tables which contain a Receipt total for ID 0001.

Table 1 has a "Receipt Total" row of data for this ID of $10 and then another row with a total of $11.

Table 2 has a "Premium Receipt Total" row of data for ID 0001 for $5 and another row with a total of $6.

In my mind I should be able to run one Select query and match these two tables by the ID# and then pull in the ID (group it) and pull in the "Receipt Total of Table 1" (sum it) and pull in the "Premium Receipt Total of Table 2" (sum it) and should get the following results.

ID 0001
Receipt Total= $21 (instead I get $42)
Premium Receipt Total-$11 (instead I get $22)

It seems to me I shouldn't have to sum each of these tables in a separate query and then create a 3rd query that pulls in these two summed tabled queries to get the correct totals.

Thanks in advance!
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Because you are creating a many-to-many relationship by joining these two tables together like this (your Join field has duplicate values on both sides).
That is not the way you want to go about this. You want a single long list of all your records. You can accomplish this with a UNION Query like this:
Code:
SELECT Table1.ID, Table1.[Receipt Total],0 as [Premium Receipt Total]
FROM Table1
UNION
SELECT Table2.ID,0 as [Receipt Total],Table2.[Premium Receipt Total]
FROM Table2;
Note that we place a zero in for the place holder, since each Total field only exists in one table, and we want to return the both of them (we want our final query to have three fields, not two).
See the following link for more on UNION Queries. You need to to create these in SQL View (cannot do them in the Query Builder Tool).
https://support.office.com/en-us/ar...on-query-3856f16c-0a22-43f2-8c23-29ec44acbc05

Then, we can use an Aggregate Query on this Union Query to get the totals you want. You can either do this is a separate query based on the Union Query we just wrote, or it can be done in a single query simply by nesting the Union Query in a Subquery like this:
Code:
SELECT ID, Sum([Receipt Total]) as SumOfReceiptTotal, Sum([Premium Receipt Total]) as SumOfPremiumReceiptTotal
FROM
(SELECT Table1.ID, Table1.[Receipt Total],0 as [Premium Receipt Total]
FROM Table1
UNION
SELECT Table2.ID,0 as [Receipt Total],Table2.[Premium Receipt Total]
FROM Table2)
GROUP BY ID;
 
Upvote 0
You are welcome.

Things always get a little trickier and are more work than they have to be when you are dealing with a data structure that is not normalized.

The other way to go about it is if you had a separate table of all your IDs, and they were unique (exactly one record per ID).
Then you could create a query with left joins from this ID table to your other two tables, and do an Aggregate Query on that.
 
Upvote 0

Forum statistics

Threads
1,221,834
Messages
6,162,268
Members
451,758
Latest member
lmcquade91

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