I have a typical question with a twist.
I need to modify the following T SQL query. Currently it only looks in one table for information. I need to look into two tables with identical columns for information. One table contains current employees and one contains former employees
SELECT t.net_Id
,e.fname
,e.lname
FROM tblTrackingEmployee t
join view_employee e
on e.net_id = t.net_id
where trackingid = @trackingId
and empType = @empType
What I was thinking of doing is using a union to look in both. An employee will either appear in one table or the other, it will never be in both.
SELECT t.net_Id
,e.fname
,e.lname
FROM tblTrackingEmployee t
JOIN view_employee e
ON e.net_id = t.net_id
WHERE trackingid = @trackingId
AND empType = @empType
union
SELECT t.net_Id
,fe.fname
,fe.lname
FROM tblTrackingEmployee t
JOIN view_employee fe
ON fe.net_id = t.net_id
WHERE trackingid = @trackingId
AND empType = @empType
However here is the twist, I need to know if the person/s returned are current or former employees. Is there a way to add a column to the returned table that has a 1 if its a current employee or 0 if its a former employee? Each trackingId can potentially contain both types of employees.
Add a constant column to each
SELECTclause – you can alias it: