Reputation: 43
I'd like to join four tables in Oracle 8i. The four table names are users, picks, putaways, and relocates. Each of the four tables has a user_id field. Picks, putaways, and relocates look like this:
| USER_ID | PICKS | PICK_VOLUME |
+---------+-------+-------------+
| SMITH12 | 234 | 2431.12 |
The putaways and relocates tables look identical except 'PICKS' and 'PICK_VOLUME' are replaced with putaways and relocates, respectively.
The table that I would like to do a left join on is the users table since it will contain all of the user_ids, regardless if they only picked inventory and didn't relocate, or put any of it away. In other words, I would like to see all the records even if some users only have relocates, instead of having picks, putaways, and relocates, or any combination thereof.
The user table looks like this:
| USER_ID | SHIFT | GROUP_ID |
+---------+-------+------------+
| SMITH12 | NIGHT | STOCK_KEEP |
So ideally, I would like to have those three columns at the left of my result set, with the picks, putaways, and relocates table joined to the right. The final select results should look like this:
| USER_ID | SHIFT | GROUP_ID | PICKS | PICK_VOL | RELOCATES | RELOCATE_VOL | PUTAWAYS | PUTAWAY_VOL |
+---------+-------+------------+-------+----------+-----------+--------------+----------+-------------+
| SMITH12 | NIGHT | STOCK_KEEP | 234 | 2431.12 | NULL | NULL | 4 | 76.52 |
I've tried this SQL query:
select users.user_id, users.group_id, users.shift, pi."Picks", pi."Pick Volume", pu."Putaways", pu."Putaway Volume", re."Relocates", re."Relocate Volume"
from users, pi, pu, re
where users.user_id = pi.user_id
and users.user_id = pu.user_id
and users.user_id = re.user_id;
but it only returns rows where a user has at least one transaction in the picks, relocates, and putaways tables. This is expected since I'm doing a vanilla join. I'm assuming I need some kind of left join, but I'm not sure how to write it in Oracle 8i.
Upvotes: 3
Views: 3277
Reputation: 298
You can use "left outer join":
select users.user_id
, users.group_id
, users.shift
, pi."Picks"
, pi."Pick Volume"
, pu."Putaways"
, pu."Putaway Volume"
, re."Relocates"
, re."Relocate Volume"
from users
LEFT OUTER JOIN pi
ON users.user_id = pi.user_id
LEFT OUTER JOIN pu
ON users.user_id = pu.user_id
LEFT OUTER JOIN re
ON users.user_id = pi.user_id
;
Upvotes: 0
Reputation: 8709
Use the (+) operator:
select users.user_id, users.group_id, users.shift, pi."Picks", pi."Pick Volume", pu."Putaways", pu."Putaway Volume", re."Relocates", re."Relocate Volume"
from users, pi, pu, re
where users.user_id = pi.user_id(+)
and users.user_id = pu.user_id(+)
and users.user_id = re.user_id(+);
http://docs.oracle.com/cd/B28359_01/server.111/b28286/queries006.htm
Upvotes: 5