Solved

Selecting attributes from two different tables which both refrence to same lookup table

Posted on 2004-10-15
4
232 Views
Last Modified: 2012-05-05
In this case I'm trying to get two different County names in the select clause from two different tables, but the attributes in the table, both CountyID, refer to the uCounty table. Ultimately I'm running into selecting uCounty.description for both.  I'm not sure if there's an alias somewhere that could help me avoid this.
Here's what I have so far as a test. The actual statement i'm using is much more complex, but proceeding query, which is very wrong, should give you an idea.

SELECT     dbo.uCounty.Description, dbo.uCounty.Description AS Expr1
FROM         dbo.tApplicant INNER JOIN
                      dbo.tWorkHistory ON dbo.tApplicant.ApplicantID = dbo.tWorkHistory.ApplicantId LEFT OUTER JOIN
                      dbo.uCounty ON dbo.uCounty.CountyId = dbo.tWorkHistory.CountyID LEFT OUTER JOIN
                      dbo.uCounty uCounty_1 ON dbo.uCounty.CountyId = dbo.tApplicant.CountyID


uCounty
-------
CountyID
Description

tApplicant
------------
ApplicantID
...
CountyID

tWorkHistory
--------------
WorkHistoryID
ApplicantID
...
CountyID
0
Comment
Question by:bradtm
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
boblah earned 500 total points
ID: 12322439
Hi bradtm,

You want

SELECT     dbo.uCounty.Description, dbo.uCounty.Description AS Expr1
FROM         tApplicant a
                    INNER JOIN tWorkHistory wh
                        ON a.ApplicantID = wh.ApplicantId
                    LEFT OUTER JOIN dbo.uCounty cwh
                        ON cwh.CountyId = wh.CountyID
                    LEFT OUTER JOIN dbo.uCounty ca
                        ON ca.CountyId = a.CountyID



Cheers!
0
 
LVL 15

Expert Comment

by:jdlambert1
ID: 12322464
SELECT     dbo.uCounty.Description, dbo.uCounty_1.Description AS Expr1
FROM        dbo.tApplicant
 INNER JOIN dbo.tWorkHistory ON dbo.tApplicant.ApplicantID = dbo.tWorkHistory.ApplicantId
 LEFT JOIN dbo.uCounty ON dbo.uCounty.CountyId = dbo.tWorkHistory.CountyID
 LEFT JOIN dbo.uCounty uCounty_1 ON dbo.uCounty.CountyId = dbo.tApplicant.CountyID
0
 
LVL 15

Expert Comment

by:jdlambert1
ID: 12322516
Your join conditions are critical to your results, but when you join to uCounty twice, and want it's description column from each virtual copy, you need to modify your SELECT clause to use the alias:

dbo.uCounty.Description, dbo.uCounty_1.Description AS Expr1
0
 

Author Comment

by:bradtm
ID: 12322594
ah yes. it's Friday...I was spacing out on the aliasing. thanks!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now