?
Solved

SQL joining multiple columns from table to a single column in another table

Posted on 2016-09-29
4
Medium Priority
?
67 Views
Last Modified: 2016-09-29
Hi

I have a table called "Performance" and it has columns called [Machine Operator], [Assistant Operator], [Assistant Operator 2] and [Assistant Operator 3]
All of the aforementioned columns hold the ID numbers for the company Employees. The Employees table has an ID column and a second column called [Full Name].
I need to pull a report of from the Performance table that shows the Employee Names not IDs. To do this I need to join the ID column in Employees to all of the other columns [Machine Operator], [Assistant Operator], [Assistant Operator 2] and [Assistant Operator 3]
What SQL code would I use too do that
0
Comment
Question by:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 51

Accepted Solution

by:
Vitor Montalvão earned 2000 total points
ID: 41821450
SELECT E1.Name AS MachineOperator, E2.Name AS AssistantOperator1, E3.Name AS AssistantOperator2,  E4.Name AS AssistantOperator3 
FROM Performance P
    LEFT JOIN Employees E1 ON E1.ID = P.[Machine Operator]
    LEFT JOIN Employees E2 ON E2.ID = P.[Assistant Operator]
    LEFT JOIN Employees E3 ON E3.ID = P.[Assistant Operator2]
    LEFT JOIN Employees E4 ON E4.ID = P.[Assistant Operator3]

Open in new window

0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41821474
In addition to Vitor's solution.. Below is the optional solution using cross apply..

SELECT MachineOperator,AssistantOperator1,AssistantOperator2,AssistantOperator3
FROM Performance P
OUTER APPLY (SELECT E1.Name MachineOperator    FROM Employees E1 WHERE E1.ID = P.[Machine Operator])a
OUTER APPLY (SELECT E2.Name AssistantOperator1 FROM Employees E2 WHERE E2.ID = P.[Assistant Operator])b
OUTER APPLY (SELECT E3.Name AssistantOperator2 FROM Employees E3 WHERE E3.ID = P.[Assistant Operator2])c
OUTER APPLY (SELECT E4.Name AssistantOperator3 FROM Employees E4 WHERE E4.ID = P.[Assistant Operator3])d

--

Open in new window

0
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 41821483
So, what's the advantage of the OUTER APPLY?
0
 

Author Closing Comment

by:Murray Brown
ID: 41821713
Great answer as always!
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

764 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