Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Problem with join???!!!

Author  Topic 

thiyait
Yak Posting Veteran

70 Posts

Posted - 2005-05-24 : 11:46:50
HI ALL;

I am working with three table and facing problem to join the three table data

For ex:
------
TABLEA,TABLEB,TABLEC

TABLEA:
------
FirstID secondID Name
111 2 John
222 2 Justin

TABLEB:
------
FirstID secondID location
111 2 Hosten
222 2 MD


TABLEC:
------
FirstID secondID country
111 2 America

I am trying this query with various changes,But didnt succeed unless we have the same firstid in all three table.

select TABLEA.name,TABLEB.location,TABLEC.country from TABLEA,TABLEB,TABLEC where tablea.secondID=2 and tablea.firstid=tableb.firstid and tablec.firstid=tablea.firstid


My expected result is as follow:
111 2 John housten america
222 2 Justin MD Null

How do i get the result in above form??

Thanks in advance

mr_mist
Grunnio

1870 Posts

Posted - 2005-05-24 : 11:54:00
SELECT tablea.firstid, tablea.secondid, tablea.name,
tableb.location, tablec.country
FROM TABLEA
LEFT OUTER JOIN tableb ON tablea.firstid = tableb.firstid and tablea.secondid = tableb.secondid
LEFT OUTER JOIN tablec ON tablea.firstid = tablec.firstid and tablea.secondid = tablec.secondid

-------
Moo. :)
Go to Top of Page

thiyait
Yak Posting Veteran

70 Posts

Posted - 2005-05-24 : 12:39:56
Thanks for quick reply mist:
Any of three table may not have firstid.Your query only works if tablec doesnt have the corresponding firstid.So please take a look at my modified example.For ex:
------
TABLEA,TABLEB,TABLEC

TABLEA:
------
FirstID secondID Name
111 2 John
222 2 Justin

TABLEB:
------
FirstID secondID location
111 2 Hosten
333 2 texas

TABLEC:
------
FirstID secondID country
111 2 America


My expected result is as follow:
111 2 John housten america
222 2 Justin Null Null
333 2 null texas Null

Any one please give me the solution???


Thanks in advance.
Go to Top of Page

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2005-05-24 : 20:14:54
This is 99.99999% guaranteed to be the most inefficient way to run this query.


DECLARE @TABLEA TABLE(FirstID INT, SecondID INT, UserName VARCHAR(55))
DECLARE @TABLEB TABLE(FirstID INT, SecondID INT, Location VARCHAR(55))
DECLARE @TABLEC TABLE(FirstID INT, SecondID INT, Country VARCHAR(55))

INSERT @TABLEA(FirstID, SecondID, UserName)
SELECT 111, 2, 'John' UNION ALL
SELECT 222, 2, 'Justin'

INSERT @TABLEB(FirstID, SecondID, Location)
SELECT 111, 2, 'Houston' UNION ALL
SELECT 333, 2, 'Texas'

INSERT @TABLEC(FirstID, SecondID, Country)
SELECT 111, 2, 'America'

SELECT
dt1.FirstID,
a.SecondID,
a.UserName,
b.Location,
c.Country
FROM
(
SELECT FirstID FROM @TABLEA UNION
SELECT FirstID FROM @TABLEB UNION
SELECT FirstID FROM @TABLEC) dt1
INNER JOIN (
SELECT FirstID, SecondID FROM @TABLEA UNION
SELECT FirstID, SecondID FROM @TABLEB UNION
SELECT FirstID, SecondID FROM @TABLEC) dt2 ON dt1.FirstID = dt2.FirstID
LEFT OUTER JOIN @TABLEA a ON dt1.FirstID = a.FirstID
LEFT OUTER JOIN @TABLEB b ON dt1.FirstID = b.FirstID
AND dt2.SecondID = b.SecondID
LEFT OUTER JOIN @TABLEC c ON dt1.FirstID = c.FirstID
AND dt2.SecondID = c.SecondID



MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page
   

- Advertisement -