Solved

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

Posted on 2016-09-29
4
36 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:murbro
  • 2
4 Comments
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 500 total points
Comment Utility
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 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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 45

Expert Comment

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

Author Closing Comment

by:murbro
Comment Utility
Great answer as always!
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

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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…
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.
Viewers will learn how the fundamental information of how to create a table.

744 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

17 Experts available now in Live!

Get 1:1 Help Now