Solved

How do I join multiple fields in one table to a single field on another table?

Posted on 2011-03-20
14
392 Views
Last Modified: 2012-05-11
I know the solution to this is going to be embarrassingly easy, but my brain seems to be stuck.  I have two tables, one called "Projects" and one called "Employees".  There are several employees associated with each project so I have fields in the project table called "Owner", "Originator", "Customer", etc. where only the employee ID is stored in the "Projects" table.  In the "Employees" table I of course have the fields for each employee, "EmployeeID", "LastName", "FirstName".  When I do the query to pull up a list of projects, I want to be able to show the owner's first and last name, the originator's first and last name, the customer's first and last name, etc.

If it was just the owner, the query would look like this:

SELECT tblProjects.ProjectName, [tblEmployees]![FirstName]+" "+[tblEmployees]![LastName] AS Owner
FROM tblEmployees INNER JOIN tblProjects ON tblEmployees.ID = tblProjects.Owner;


I can't figure out how to display the originator and customer similarly.

Also, if anyone who answers this would tell me how I could have asked this question better I would appreciate it.

0
Comment
Question by:MontanaProspector
  • 9
  • 2
  • 2
  • +1
14 Comments
 

Author Comment

by:MontanaProspector
ID: 35175814
I realize I've mixed Microsoft Access syntax in there, but I'm sure anyone reading this gets the drift.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35175830
I can't remember if this worked (I though I tried it once) and I don't have a way to test it right now, but when you want to join on many-to-many, you use an "AND" in your "ON" clause--what if you try using an "OR"?

SELECT tblProjects.ProjectName, [FirstName]+' '+[LastName] AS Owner
FROM tblEmployees
INNER JOIN tblProjects
ON tblEmployees.ID = tblProjects.Owner OR tblEmployees.ID = tblProjects.Originator OR tblEmployees.ID = tblProjects.Cutsomer;

Open in new window


Alternatively, instead of actually using the word "JOIN", you could just list your multiple tables and "join" them by way of the "WHERE" clause.

SELECT tblProjects.ProjectName, [FirstName]+' '+[LastName] AS Owner
FROM tblEmployees, tblProjects
WHERE tblEmployees.ID = tblProjects.Owner OR tblEmployees.ID = tblProjects.Originator OR tblEmployees.ID = tblProjects.Cutsomer;

Open in new window

0
 

Author Comment

by:MontanaProspector
ID: 35175837
Well so far I've been able to solve it using multiple views, each depending on the previous and using unique aliases for the names at every stage.  So there's a view called "vwOwner" that uses all of the fields from "tblProjects" joined to "tblEmployees" on Owner=EmployeeID with LastName and FirstName aliased as OwnerLast and OwnerFirst.  Then there's a view called "vwOriginator" that uses all of the fields from "vwOwner" joined to "tblEmployees" on Originator=EmployeeID with LastName and FirstName aliased as OriginatorLast and OriginatorFirst. And so on.  This seems a bit clunky to me (especially since the Employee table resides on a completely different server).  Is there a better way to do it?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:MontanaProspector
ID: 35175865
Thanks for your reply on a Sunday, kaufmed.  Neither of your examples are going to give me the first and last names of all three people involved, Owner, Originator, Customer, for each project.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35175907
Sure they will, but they will show as different rows, not one single row. Is that what you are trying to achieve?
0
 

Author Comment

by:MontanaProspector
ID: 35175968
Hmmm.  You're correct, they are showing up in different rows (for some reason only 2 of the three are for me, but I'm sure it's just a typo) but they are all listed as "Owner".

I'm trying to make each project come up as a single row, with Owner, Originator, and Customer listed (and differentiated) in each row.
0
 

Author Comment

by:MontanaProspector
ID: 35175976
Oh, and I just figured out why only 2 of the 3 people are showing up...so far the Originator and the Customer have been the same for each project.  I want to have provisions for those people to be different.
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 35176118
you will have to join  the person table several times ...

SELECT p.ProjectName
  , o.[FirstName] + o.[LastName] AS Owner
  , r.[FirstName] + r.[LastName] AS Originator
FROM tblProjects p
INNER JOIN tblEmployees o
  ON o.ID = e.Owner
INNER JOIN tblEmployees r
  ON r.ID = e.Originator

etc

Open in new window

0
 

Author Comment

by:MontanaProspector
ID: 35176469
Oh baby.  That is SOOOO much nicer than the way I was doing it.  Thanks angellll.

As an SQL noob, it looks to me like this wouldn't work without using aliases correct?  Or am I thinking of this right?  Is o an alias for the employee table or the JOIN?

Also, did I state my question in such a way that you could understand what I was asking without having to read the question?  How would you have asked it differently?

0
 

Author Closing Comment

by:MontanaProspector
ID: 35176493
He hit it out of the park, as usual.
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 35176531
To answer your last questions: Yes, o and r are aliases, and as they are not only used in the join but also in the list of fields selected, they are not aliases for the join (there is no such thing) they are table aliases. The verbose syntax is tablename AS tablealias (here for example "tblProjects AS p" would also be valid). This is just like with aliasnames of fields (here: AS Owner and AS Originator).

And in regard to the question: It was understandable, you just didn't specify how you would have liked the different persons to appear: In the same or additional records of the result.

What you do here simply is a multiple join of the same table. It's still only one physical table, but with two aliases o and r, which each applying to the join condition. You will have it hard to use the same table more than once without an alias :). Yes, it's a must, otherwise you couldn't give different join conditions and adress the employee being the owner vs. being the originator etc.

Bye, Olaf.
0
 

Author Comment

by:MontanaProspector
ID: 35177419
Thanks for the additional follow-up Olaf.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35178583
glad I could help.

the question was easily understandable to people having the experience of having done this kind of thing already.

for others, and usually for most questions, sample data with the relevant output requested can help to clarify much easier than tons of words and trying to explain in plain english :)

CHeers
0
 

Author Comment

by:MontanaProspector
ID: 35180422
If I could give an example of the desired output, I wouldn't have needed to ask the question <grin>.

Thanks you guys for helping me make EE a better place to find answers.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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…
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.

778 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