Solved

SQL syntax - multiple joins

Posted on 2013-11-18
2
279 Views
Last Modified: 2013-11-19
Hi,

I have a query with an inner join pulling data from two tables. One of the tables has several fields containing User IDs. This is a job table, so there is createdbyuserID, assignedtoUserID, etc. I'd like to append the existing query to pull in the names of these folks from the Users table, but I'm not sure how I would join the same table twice or even 4 times to pull names from the same set of user records.

My current query like so:

SELECT Customers.CustomerID, Customers.CustomerFirstName, Customers.CustomerLastName, Customers.CustomerEmail, Customers.CustomerPhoneCell, Customers.CustomerPhoneHome, Customers.OKContactLater, Jobs.JobAddress1, Jobs.JobAddress2, Jobs.JobCity, Jobs.JobState, Jobs.JobZipCode, Jobs.AcceptedPrice, Jobs.PaintStartDate, Jobs.PaintEndDate FROM Customers INNER JOIN Jobs ON Customers.CustomerID = Jobs.CustomerID WHERE PaintEndDate between 1/1/2013 and 12/31/2013

Open in new window


If I wanted to key on Jobs.CreatedByID to pull in Users.FirstName and Users.LastName and return that, then use Jobs.AssignedToID to pull in Users.FirstName and Users.LastName for that second different ID, how would I wrap my joins?

Thank you

Bill
0
Comment
Question by:billium99
[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 Comments
 
LVL 22

Accepted Solution

by:
Steve Wales earned 500 total points
ID: 39657906
Join the query to the Users Table twice - aliasing it the first time as Users1 and the second time as Users2:

SELECT Customers.CustomerID, Customers.CustomerFirstName, Customers.CustomerLastName, Customers.CustomerEmail, Customers.CustomerPhoneCell, Customers.CustomerPhoneHome, Customers.OKContactLater, Jobs.JobAddress1, Jobs.JobAddress2, Jobs.JobCity, Jobs.JobState, Jobs.JobZipCode, Jobs.AcceptedPrice, Jobs.PaintStartDate, Jobs.PaintEndDate, Jobs.CreatedbyID, Users1.First_name, Users2.LastName, Jobs.AssignedToID, Users2.FirstName, Users2.LastName
FROM Customers 
INNER JOIN Jobs ON Customers.CustomerID = Jobs.CustomerID 
INNER JOIN Users Users1 on Jobs.CreatedByID = Users1.UserID
INNER JOIN Users Users2 on Jobs.AssignedToID = Users2.UserID
WHERE PaintEndDate between 1/1/2013 and 12/31/2013

Open in new window

0
 
LVL 1

Author Closing Comment

by:billium99
ID: 39659512
Perfect - thx!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

628 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