有2个表Employee和Timing 输入是
EMPCODE LOGIN LOGOUT TRANSDATE 1001 9.30 17.30 20/01/2014 1002 1.30 22.30 20/01/2014 EXPECTED OUTPUT **EMPCODE INOUT(Login/Logout) TRANSDATE** 1001 9.30 20/01/2014 1001 17.30 20/01/2014 1002 10.30 21/01/2014 1002 22.30 21/01/2014 Please provide sql code?
答案 0 :(得分:3)
您可以使用CROSS APPLY取消删除列:
SELECT T.EmpCode, i.InOut, T.TransDate
FROM Timing T
CROSS APPLY (VALUES (T.Login), (T.Logout)) i (InOut);
<强> Example on SQL Fiddle 强>
您也可以使用UNPIVOT:
SELECT upvt.EmpCode, upvt.InOut, upvt.TransDate
FROM Timing T
UNPIVOT
( InOut
FOR [Type] IN (Login, Logout)
) upvt;
<强> Example on SQL Fiddle 强>
答案 1 :(得分:1)
试试这个: -
Select Empcode,[InOUT],[TRANSDATE]
from
(
Select EmpCode, Login as [INOUT] ,[TRANSDATE]
from Table1
union all
Select EmpCode, Logout as [INOUT] ,[TRANSDATE]
from Table1
)a
order by empcode