Simple Join (maybe?)

Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2009
Posts: 2
Reputation: Summerston is an unknown quantity at this point 
Solved Threads: 0
Summerston Summerston is offline Offline
Newbie Poster

Simple Join (maybe?)

 
0
  #1
Apr 14th, 2009
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 +
++++++++++++++++++++++++++++++++++++
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 793
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Simple Join (maybe?)

 
0
  #2
Apr 14th, 2009
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:
  1. -- first select the columns required
  2. SELECT wo.id, em.name as submitted_by, emp.name as completed_by
  3. -- not sure if you can use these aliases
  4. -- because they are also column names of the WorkOrders table
  5. -- if not, rename them to submitted_name and completed_name for example
  6.  
  7. -- from tables
  8. FROM WorkOrders wo
  9. INNER JOIN employee em
  10. -- link submitted_by to id of employee
  11. ON wo.submitted_by = em.id
  12. INNER JOIN employee emp
  13. -- link completed_by to id of employee
  14. 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Simple Join (maybe?)

 
0
  #3
Apr 14th, 2009
Here you're
  1. SELECT WorkOrders.ID, employees.name, employees_1.name AS Expr1
  2. FROM WorkOrders LEFT OUTER JOIN
  3. employees AS employees_1 ON WorkOrders.submitted_by = employees_1.id LEFT OUTER JOIN
  4. 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
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 2
Reputation: Summerston is an unknown quantity at this point 
Solved Threads: 0
Summerston Summerston is offline Offline
Newbie Poster

Re: Simple Join (maybe?)

 
0
  #4
Apr 14th, 2009
Thanks guys. Both where sufficient.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC