I have three tables in Microsoft Access:
- Table1 includes the names of various companies and the corresponding unique ID of each (along with other unique information about the company);
- Table2 includes sales information for each company from 2013;
Table3 includes sales information for each from 2014.
In Tables 2 & 3, a single company is often listed more than once, corresponding to sales in different states.
In Tables 2 & 3, the company name is actually its corresponding ID number, and I'm using a lookup to display the name. Thus, Table1 is the parent of Table2 and Table3.
I want to create a query that lists each company exactly once along with the sum of their sales from Tables 2 & 3. This works fine when I only try to use Table2 OR Table3, but when I incorporate both, the sales are inflated by a constant scalar (a company will show sales exactly 16X what they should be, for example). What's going on here?
Here's what the SQL shows in Access:
SELECT Table2.CompanyName,
Sum(Table2.TotalRevenue) AS [2013 Revenue],
Sum(Table3.TotalRevenue) AS [2014 Revenue]
FROM (Table1 INNER JOIN Table2 ON Table1.Company_ID = Table2.CompanyName)
INNER JOIN Table3 ON Table1.Company_ID = Table3.CompanyName
GROUP BY Table2.CompanyName;
Why is it pulling these Revenue values multiple times? Thank you!
First you may consider merge
TABLE2andTABLE3together and just add aYEARfield. If you keep growing one table for year will be messy in the future.the problem is you are performing a cartesian product.
You need
Not sure what is the proper syntaxis in Access but you need a calculate totals on the subquery.
And you need
LEFT JOINin case the company didnt sell on that year. (Like a company join on 2014) andCOALESCEto convertNULLto0.