473,419 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

Joining Databases with Left Outer Joins

Hey All,

I really need some help here!

I currently have 3 databases that I am writing a script for to extract data. I need to join these databases together to get one extract, but I have a whole bunch of data that I need and from different tables in the database. Therefore I have left outer joins to join up tables in there.

SQL won't let me join the databases if I have left outer joins as well, but I have only been trying a Union.

Any suggestions are welcome :)
Nov 23 '10 #1
7 1589
yarbrough40
320 100+
more specifics please.

what have you tryed so far? what do you mean --> "SQL won't let me join the databases if I have left outer joins"

what kind of errors are you getting?

present some examples.
Nov 24 '10 #2
Sorry - I'm only learning SQL at the moment so I might not have it right, but this is an example:

use databasea
go

select
employee.employeecode,
employee.firstnames,
employee.lastname,
employee.locationcode,
location.description,
employee.positioncode,
position.description

from employee
left outer join location location on
(employee.locationcode = location.locationcode)
left outer join position position on
(employee.positioncode = position.positioncode)

where employee.terminationdate is NULL

I have written a script to join the databases using a union before, but if I try this and use a left outer join it doesn't work:

select
ea.employeecode,
ea.firstnames,
ea.lastname,
ea.departmentcode,
ea.locationcode,
ea.Xdatabase

from databasea.dbo.Employee ea

where ea.terminationdate is NULL
AND ea.EmployeeCode NOT LIKE '[_]%'

union

select
eb.employeecode,
eb.firstnames,
eb.lastname,
eb.departmentcode,
eb.locationcode,
eb.Xdatabase

from databaseb.dbo.Employee eb

where eb.terminationdate is NULL
AND eb.EmployeeCode NOT LIKE '[-]%'


Please excuse my very basic scripts, I'm just a beginner :)
Nov 24 '10 #3
yarbrough40
320 100+
the administrators are going to ask you to put your code in code tags when you post. It's the "#" sign in the messagebox menu bar.

So, I'm still confused here. you are showing two queries. One with Left Joins and One with a Union. Which one exactly is failing? and again, what errors are you getting?
Nov 25 '10 #4
Sorry - so I have figured out how to join the databases now, but the join isn't working on the third database although it isn't failing as the employees are there, it just has NULL in the cell. I have just written the below basic formula:

use databasea
go

select
employee.employeecode,
employee.firstnames,
employee.lastname,
employee.locationcode,
location.description,
employee.positioncode,
position.description

From
(
Select
ea.employeecode,
ea.firstnames,
ea.lastname,
ea.locationcode,
ea.positioncode,
ea.terminationdate

from databasea.dbo.employee ea

union

select
eb.employeecode,
eb.firstnames,
eb.lastname,
eb.locationcode,
eb.positioncode,
eb.terminationdate

from databaseb.dbo.employee eb

union

select
ec.employeecode,
ec.firstnames,
ec.lastname,
ec.locationcode,
ec.positioncode,
ec.terminationdate

from databasec.dbo.employee ec

)

employee
left outer join location location on
(employee.locationcode = location.locationcode)
left outer join position position on
(employee.positioncode = position.positioncode)

where employee.terminationdate is NULL

order by employee.employeecode

The above were two different scripts, I was just trying to show what I wanted to be joined and then also how I have joined the databases with a union before.

The above script should make more sense.

Thanks
Nov 26 '10 #5
Sorry I mean't the last reply (#3) was two different scripts.
Nov 26 '10 #6
Ahhh... I have just realised that my databases are not exactly the same and therefore there are NULL's because the fields aren't set up in some of the databases.

Is this the best way to join them?
Nov 26 '10 #7
yarbrough40
320 100+
So are you saying your queries are working now? If so then you are fine with the way you have them constructed.

the only thing I see is that I don't believe you need to repeat your table name

this is yours:
Expand|Select|Wrap|Line Numbers
  1. left outer join location location on
  2.  
you only need this:
Expand|Select|Wrap|Line Numbers
  1. left outer join location on
  2.  
Nov 26 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Dam | last post by:
Using SqlServer : Query 1 : SELECT def.lID as IdDefinition, TDC_AUneValeur.VALEURDERETOUR as ValeurDeRetour FROM serveur.Data_tblDEFINITIONTABLEDECODES def,...
1
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
7
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
3
by: Ian Boyd | last post by:
i know nothing about DB2, but i'm sure this must be possible. i'm trying to get a client to create a view (which it turns out is called a "Logical" in DB2). The query needs a LEFT OUTER JOIN, but...
5
by: Dev | last post by:
Hello all, I need to do a left out join where a.field1 ilike %b.field2% But I can not figure out the exact syntax to using the ilike in the join? ----------
4
by: Brian Parker | last post by:
I'm new to ms-sqlserver ( 2000 ) and need to get an OUTER JOIN working on a three table query. Assumptions: -- I have events in the Event table. -- Each event CAN have one Transaction, but it's...
3
by: media.opslag | last post by:
Hi, How can i get this to work in access / jet sql ??? Someone?? SELECT tbl1., tbl2. FROM tbl1 left outer join tbl2 on
0
by: ibadba | last post by:
Hello all, today I think I'm stupid, can anyone confirm?? I have a query on multinode DB2 ( v8.2 Aix ), as follows: select a.col1,b.col1,c.col1 from table1 a left outer join table2 b on
1
by: Mickey001 | last post by:
Greetings all, I need some help in writing and understanding a sql query for the following: Here is a simplified scenario: 2 Tables: First Table Name =============
3
by: nico3334 | last post by:
I currently have a query that Joins 2 Tables (Table1 and Table2) using LEFT OUTER JOIN. Here is an example of that query: SELECT a.supply, a.state, b.cost FROM Table1 a LEFT...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.