Access Newbie - can't work this out.

amthorpe2000

New Member
Joined
Aug 29, 2009
Messages
8
I have 3 tables which are imported from Excel. Each table has many columns but 3 are consistent (team, month, value).

1st i want to get the distinct / unique combinations accross all 3 tables for the team and month, which i did with a union query (I think)

SQL:
SELECT team, period
FROM Table3

UNION SELECT team, period
FROM Table1

UNION SELECT team, period
FROM Table2
ORDER BY team, period;

but then I want the sum of the value from each of the 3 tables against the unique combination. So each table may have 1000's of records, which i want to end up with the query results in the right hand red set in the image
accessquery.PNG


Everything i try comes up with data mismatch or produces 100's more entries than the original.

Any help appreciated
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
My guess would be that you'd use a Select query, joining 3 tables on Team (outer joins?), having 3 value fields (1 from each table) and then create a Totals Query and group by Team. The reason being that you want the totals query to transform the value fields by table. That's just a guess.
Or a totals query based on your Union query. However, I think your main problem is that you appear to have 3 identical tables when you should have only one. Methinks you've designed your tables as you would spreadsheets, which unfortunately is very common when Excel users migrate to Access. I'd suggest you research db normalization if you're not familiar with the concepts because if I'm right, your design will continue to frustrate you and in some cases it will present insurmountable problems.
 
Upvote 0
My guess would be that you'd use a Select query, joining 3 tables on Team (outer joins?), having 3 value fields (1 from each table) and then create a Totals Query and group by Team. The reason being that you want the totals query to transform the value fields by table. That's just a guess.
Or a totals query based on your Union query. However, I think your main problem is that you appear to have 3 identical tables when you should have only one. Methinks you've designed your tables as you would spreadsheets, which unfortunately is very common when Excel users migrate to Access. I'd suggest you research db normalization if you're not familiar with the concepts because if I'm right, your design will continue to frustrate you and in some cases it will present insurmountable problems.
Maybe, the three tables are from spreadsheets of data that are for different snapshots over time, what i was hoping was that to avoid VBA in a particular spreadsheet which i could do that i could store the "heavy" data in a workbook, pull it into Access get access to sum it up and then import the resulting query into another workbook. I have done quite a lot of access years ago, but this is eluding me.
 
Upvote 0
Lacking a complete understanding of the process I'm left to wonder why not just do the summing in Excel since doing complex calculations is what it's made for? If importing/linking within Access is necessary, a typical approach is to link the spreadsheets as tables then use queries to move the data to the properly designed table(s). One could use transferspreadsheet function as well, but sometimes that fudges the data types, so for example, you get text instead of numbers. That would also give you the option to overwrite or just append.
 
Upvote 0
Change your union query to include value field too and use UNION ALL then use that query as source of a group query with it being grouped by team and period and sum of the value... Untested:

SQL:
SELECT team, period, sum([value]) AS TotalValue
FROM

(SELECT team, period, [value] 
FROM Table3

UNION ALL SELECT team, period, [value] 
FROM Table1

UNION ALL SELECT team, period, [value] 
FROM Table2) MyUnion

GROUP BY
team, period;

I have assumed your field is called value, however this is a reserved word within Access and shouldn't be used for field names.
 
Upvote 0
Sorry, missed the totals by table part out... Still untested.

SQL:
SELECT team, period,
sum(iif(source = "T1", [value], 0) AS T1Total,
sum(iif(source = "T2", [value], 0) AS T2Total,
sum(iif(source = "T3", [value], 0) AS T3Total,
sum([value]) AS TotalValue

FROM

(SELECT team, period, [value], "T3" as source FROM Table3

UNION ALL SELECT team, period, [value], "T1" as Source FROM Table1

UNION ALL SELECT team, period, [value], "T2" as Source FROM Table2) MyUnion

GROUP BY team, period;
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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