Solved

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

Posted on 2016-09-29
4
45 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 46

Accepted Solution

by:
Vitor Montalvão earned 500 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 24

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 46

Expert Comment

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

Author Closing Comment

by:murbro
ID: 41821713
Great answer as always!
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

914 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

15 Experts available now in Live!

Get 1:1 Help Now