Solved

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

Posted on 2011-03-20
14
367 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 74

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
 

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 74

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
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.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

746 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

13 Experts available now in Live!

Get 1:1 Help Now