Solved

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

Posted on 2004-10-15
4
240 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

820 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