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
 General SQL Server Forums
 New to SQL Server Programming
 Joining a Table with Itself

Author  Topic 

Plotin
Starting Member

21 Posts

Posted - 2005-12-06 : 13:01:20
If a table gets joind with itself and then joined again with itself, is it possible to perform one kind of outer join from the third table to the second tabe and having the range of records in the second table limited to the joins between the first and the second table?





Join on RegionID Join on RegionID and City
Show records not qualifying for the joint
but limited to the scope of of records
established by the first joint
_______________ ________________ _______________
| | | | | |
| RegionID One | | RegionID One | | RegionID One |
|_______________| | City New York | | City New York|
|________________| |_______________|
_______________ ________________ _______________
| | | | | |
| RegionID One | | RegionID One | | RegionID One |
|_______________| | City New York | | City New York |
|________________| |_______________|
_______________ ________________ _______________
| | | | | |
| RegionID One | | RegionID One | | RegionID One |
|_______________| | City Buffalo | | City New York |
|________________| |_______________|

Plotin

Plotin
Starting Member

21 Posts

Posted - 2005-12-06 : 13:10:59
Formatting of the posting above is pretty messed up.
Can someone tell me how to use HTML in a message?

Below is a select statement which hopefuly exemplifies what is tried to be asked

select rgtwo.regionid,rgtwo.city,rgthree.regionid,rgthree.city
from Region as rgone(nolock) join Region as rgtwo(nolock)
on rgone.regionid = rgtwo.regionid
and rgone.regionid = 1 left outer join region as rgthree(nolock)
on rgtwo.regionid = rgthree.regionid
and rgthree.city = 'New York'

Thanks

Plotin
Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2005-12-06 : 15:35:12
HTML in a message: Click the "Forum Code" link to the left of the Message text box for help.

Notice how [ code ] will format your query:

select rgtwo.regionid,rgtwo.city,rgthree.regionid,rgthree.city
from Region as rgone(nolock) join Region as rgtwo(nolock)
on rgone.regionid = rgtwo.regionid
and rgone.regionid = 1 left outer join region as rgthree(nolock)
on rgtwo.regionid = rgthree.regionid
and rgthree.city = 'New York'
Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2005-12-06 : 15:37:55
It's a personal style thing, but I like to see my tables in a column like this:

select rgtwo.regionid,rgtwo.city,rgthree.regionid,rgthree.city
from Region as rgone(nolock)
join Region as rgtwo(nolock)
on rgone.regionid = rgtwo.regionid
and rgone.regionid = 1
left outer join region as rgthree(nolock)
on rgtwo.regionid = rgthree.regionid
and rgthree.city = 'New York'


Shouldn't the first join be an INNER JOIN or LEFT OUTER JOIN?
Go to Top of Page
   

- Advertisement -