Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outer join or Union or

Status
Not open for further replies.

GPnew

Programmer
Nov 12, 2001
56
NZ
This is a cut down version of my problem. I am sure the answer is so simple
I have a sales and budget table
Sales
ID Period Sales
1 1 200
2 3 300
3 4 500

ID Period Budget
1 1 100
2 2 400
3 4 50

I would like a query to produce

Peron Sales Budget
1 200 100
2 0 400
3 300 0
4 500 50

Please help
Thanks

 
You could first create helper query (union, distinct) to get all existing periods. If it is a fixed set, you can create table with periods instead. Next outer join this query (or table) with sales and budget tables.
Alternatively union sales and budget tables, with adding description column (Sales or Budget). Next create crosstab query. Drawback: nulls instead zeros, poor input for reports and other queries.

combo
 
I did my phd (piled higher & deeper) on oyher queries.

BTW, GPnew, welcome to Tek-Yips.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
I love crosstabs but I think you can accomplish this with a UNION and a TOTALS query. Your union query would look like:
Code:
SELECT Period, SALES, 0 as Budget
FROM Sales
UNION SELECT Period, 0, Budget
FROM Budget

Then create a totals query:

SQL:
SELECT Period, Sum(Sales) as TotSales, Sum(Budget) as TotBudget
FROM quniSalesBudget
GROUP BY Period

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top