?
Solved

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

Posted on 2016-09-29
4
Medium Priority
?
80 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
  • 2
4 Comments
 
LVL 53

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 38

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 53

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

807 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