Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Joining 2 tables

Author  Topic 

fchachar
Starting Member

15 Posts

Posted - 2012-09-24 : 14:45:40
HI, I have 2 tables that I have joined

Table1: WorkItem
Table2: Worker

Workitem is the main table where Worker table contains information about the person such as name, userid etc. Workitem has two fields 1, Assigned by 2, Modified by both fields have userids.

I want to capture the names Assigned by and Modified by.

select * from workitem
left join worker on workitem.assignedtoid = worker.id

please suggest.

Thanks.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-09-24 : 14:51:35
You will need to join to the worker table twice:
SELECT wi.*,
w1.[name] AS AssignedToLastname,
w2.[name] AS AssignedByLastname
FROM workitem wi
LEFT JOIN worker w1
ON w1.id = wi.assignedtoid
LEFT JOIN worker w2
ON w2.id = wi.assignedbyid
Go to Top of Page

fchachar
Starting Member

15 Posts

Posted - 2012-09-24 : 15:14:45
Thanks alot Sunitabeck.. you're the best
Go to Top of Page
   

- Advertisement -