Ms query - left join with multiple tables

largeselection

Active Member
Joined
Aug 4, 2008
Messages
358
Hello,

So as part of a VBA project I'm working with in Excel, I am trying to simplify some SQL writing, but I can't get the Left Joins to work.

Basically, I have many tables and I want to be able to pull information from all of them. They are set up in the following way.

MASTER_TABLE:
[ID#], [NAME], [ADDRESS]

FEBRUARY_TABLE:
[ID#], [PURCHASES]

MARCH_TABLE:
[ID#], [PURCHASES]

APRIL_TABLE:
[ID#], [PURCHASES]

ETC BY MONTHS

There may be some ID#s that are in FEB, but not MARCH, or in MARCH, but not APRIL or FEB, etc...

Ideally I wanted to do a full outer join between ID# in all four (or more) tables, but I think this might be too complex.

So as an alternative it occured to me that since my MASTER_TABLE should contain all the possible [ID#]s I could probably accomplish this with a left join.

So I can't do this graphically, but I think it is possible with SQL (I am a very beginner with SQL). I want to be able to pull a query which lists all the [ID#]s from the MASTER_TABLE and then the [PURCHASES] for FEB, MARCH, AND APRIL.

Tables might look like this:
MASTER_TABLE
ID/ADDRESS
101/XXX
102/YYY
103/ZZZ
104/AAA
105/BBB
106/CCC
107/DDD

FEB_TABLE
ID/PURCHASES
101/50
102/25
103/35

MAR_TABLE
101/25
104/50
105/10

APR_TABLE
101/50
106/10
107/100

And I would want the query to look like:
ID/FEB/MAR/APR
101/50/25/50
102/25/0/0
103/35/0/0
104/0/50/0
105/0/10/0
106/0/0/10
107/0/0/100

Any idea how to get this done? Or any helpful links/books I should consult?

Thanks!
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
If you can, can you start again with different tables? Not one table for each month! The present structure is bad. If you change to a good data structure, it will be MUCH MUCH easier in the long run.
 
Upvote 0
For a result like at the end of the first post, use a cross tab query. Something like

TRANSFORM Sum(Purchase)
SELECT ID
FROM table
GROUP BY ID
PIVOT PurchaseMonth

There will be examples in old forum posts. OK?
 
Upvote 0
And if you wanted, you can specify the months. With,

TRANSFORM Sum(Purchase)
SELECT ID
FROM table
GROUP BY ID
PIVOT PurchaseMonth IN (list the months you want in the order you want)
 
Upvote 0
Yea I definitely read that article from ms on joins. I will definitely check out the resource you provided and I purchased a basic book on SQL. Thanks so much!
 
Upvote 0

Forum statistics

Threads
1,215,761
Messages
6,126,735
Members
449,333
Latest member
Adiadidas

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