Solved

Join on the same table different column

Posted on 2012-03-25
4
607 Views
Last Modified: 2012-03-25
Is it possible to JOIN on the same table, but different column in the same query?

SELECT Table1.Name, Table2.FirstName
FROM Table1
INNER JOIN Table2
ON Table1.Table1ID = Table2.FirstID
INNER JOIN Table2
ON Table1.Table1ID = Table2.SecondID

I know the above scenario doesn't work, so how could I JOIN on the same table different column?
0
Comment
Question by:mitdaniels
  • 2
  • 2
4 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 37764133
use a LEFT JOIN

SELECT Table1.Name, Table2.FirstName
FROM Table1
INNER JOIN Table2
ON Table1.Table1ID = Table2.FirstID
LEFT JOIN Table2
ON Table1.Table1ID = Table2.SecondID
0
 

Author Comment

by:mitdaniels
ID: 37764219
Thanks aneeshattingal.

I gave it a go, but get this error message:

"Msg 1013, Level 16, State 1, Line 1
The objects "Table2" and "Table2" in the FROM clause have the same exposed names. Use correlation names to distinguish them."
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 250 total points
ID: 37764272
SELECT Table1.Name, Table2.FirstName
FROM Table1
INNER JOIN Table2  
ON Table1.Table1ID = Table2.FirstID
LEFT JOIN Table2 t
ON Table1.Table1ID = t.SecondID
0
 

Author Closing Comment

by:mitdaniels
ID: 37764287
The concept works thanks!!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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, show how to shrink a transaction log file down to a reasonable size.

770 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