| | |
Simple Join (maybe?)
Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2009
Posts: 2
Reputation:
Solved Threads: 0
What SQL statement would I need to join the employees/work orders tables to create the desired output (below)?
++++++++++++++++++++
+ employees +
++++++++++++++++++++
+ id | name +
++++++++++++++++++++
+ 1 | Jim Smith +
+ 2 | Susie Helms +
++++++++++++++++++++
++++++++++++++++++++++++++++++++++++
+ Work Orders +
++++++++++++++++++++++++++++++++++++
+ id | submitted_by | completed_by +
++++++++++++++++++++++++++++++++++++
+ 1 | 1 | 2 +
+ 2 | 2 | 1 +
+ 3 | 2 | 2 +
++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++
+ Desired Output +
++++++++++++++++++++++++++++++++++++
+ id | submitted_by | completed_by +
++++++++++++++++++++++++++++++++++++
+ 1 | Jim Smith | Susie Helms +
+ 2 | Susie Helms | Jim Smith +
+ 3 | Susie Helms | Susie Helms +
++++++++++++++++++++++++++++++++++++
++++++++++++++++++++
+ employees +
++++++++++++++++++++
+ id | name +
++++++++++++++++++++
+ 1 | Jim Smith +
+ 2 | Susie Helms +
++++++++++++++++++++
++++++++++++++++++++++++++++++++++++
+ Work Orders +
++++++++++++++++++++++++++++++++++++
+ id | submitted_by | completed_by +
++++++++++++++++++++++++++++++++++++
+ 1 | 1 | 2 +
+ 2 | 2 | 1 +
+ 3 | 2 | 2 +
++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++
+ Desired Output +
++++++++++++++++++++++++++++++++++++
+ id | submitted_by | completed_by +
++++++++++++++++++++++++++++++++++++
+ 1 | Jim Smith | Susie Helms +
+ 2 | Susie Helms | Jim Smith +
+ 3 | Susie Helms | Susie Helms +
++++++++++++++++++++++++++++++++++++
Hi Somerston and welcome to DaniWeb 
You will need to join to the employee table twice to get the name for each column. Something like this should work:

You will need to join to the employee table twice to get the name for each column. Something like this should work:
sql Syntax (Toggle Plain Text)
-- first select the columns required SELECT wo.id, em.name as submitted_by, emp.name as completed_by -- not sure if you can use these aliases -- because they are also column names of the WorkOrders table -- if not, rename them to submitted_name and completed_name for example -- from tables FROM WorkOrders wo INNER JOIN employee em -- link submitted_by to id of employee ON wo.submitted_by = em.id INNER JOIN employee emp -- link completed_by to id of employee ON wo.completed_by = emp.id
Last edited by darkagn; Apr 14th, 2009 at 10:26 am. Reason: welcome!
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend. Here you're
sql Syntax (Toggle Plain Text)
SELECT WorkOrders.ID, employees.name, employees_1.name AS Expr1 FROM WorkOrders LEFT OUTER JOIN employees AS employees_1 ON WorkOrders.submitted_by = employees_1.id LEFT OUTER JOIN employees AS employees ON WorkOrders.completed_by = employees.id
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
![]() |
Similar Threads
- formulating a sql query.Its urgent ! (MS Access and FileMaker Pro)
- update two rows in one query (MS SQL)
- problem for a simple asp.net query (ASP)
- Urgent help required (Oracle)
- Join the greatest community on Earth with M6.Net’s awesome Internet offer! (Web Hosting Deals)
- todaysfishing.com (Website Reviews)
- How do i get ppl to join my forum (Website Reviews)
- Question about join of thread. (Java)
Other Threads in the MS SQL Forum
- Previous Thread: determining if an SQL database exists, etc.
- Next Thread: Update one table with another tables values
| Thread Tools | Search this Thread |






