943,882 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Marked Solved
  • Views: 579
  • MS SQL RSS
Apr 14th, 2009
0

Simple Join (maybe?)

Expand Post »
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 +
++++++++++++++++++++++++++++++++++++
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Summerston is offline Offline
2 posts
since Apr 2009
Apr 14th, 2009
0

Re: Simple Join (maybe?)

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:
sql Syntax (Toggle Plain Text)
  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!
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Apr 14th, 2009
0

Re: Simple Join (maybe?)

Here you're
sql Syntax (Toggle Plain Text)
  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
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Apr 14th, 2009
0

Re: Simple Join (maybe?)

Thanks guys. Both where sufficient.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Summerston is offline Offline
2 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: determining if an SQL database exists, etc.
Next Thread in MS SQL Forum Timeline: Removing duplicates from INNER JOIN of two tables





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC