I am new to MS SQL so please bear with me.
I want to get the Company Code and Company Short Name of an employee DEPENDS on it's each employee status and stored it into one table.
If Emp_Status = FT
, it will get Co_Code
and Co_Shortname
from Table 1
. ELSE if Emp_Status = PT
, it will get Co_Code
and Co_Shortname
from Table 2
where USERNAME is equals to each other.
I have two tables.
Table 1:
EmpID |Co_Code |Co_ShortName |Emp_Status
WC11 |01 |AA1 |FT
KC22 |02 |BB1 |PT
MA33 |03 |CC1 |FT
Table 2:
EmpID |Co_Code |Co_ShortName |Emp_Status
WC11 |04 |DD1 |FT
KC22 |05 |EE1 |PT
MA33 |06 |FF1 |FT
Output will be
EmpID |Co_Code |Co_ShortName |Emp_Status
WC11 |01 |AA1 |FT
KC22 |05 |EE1 |PT
MA33 |03 |CC1 |FT
I am not sure if I need to use CASE or IF statement as I am getting other fields from table 1.
I know this isn't correct but this is what I have:
SELECT EmpID , CASE
WHEN Table1.EmpStatus = 'FT'
THEN (SELECT Co_Code from Table 1)
WHEN Table1.EmpStatus = 'PT'
THEN (SELECT Co_Code from Table 2 where Table1.EmpID = Table2.EmpID)
END
Please help
Emp_Status
in the table beside theFT
andPT
?