[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2016-09-29
4
Medium Priority
?
71 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 52

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 32

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 52

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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

649 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