我正在尝试开发一个考勤应用程序,根据Clock IN OUT数据计算班次细节,以下是表格详情
CREATE TABLE [dbo].[INOUTData](
EmpID VARCHAR(10),
CLockDate Date NULL,
[INTIME] Time NULL,
[OUTTIME] Time NULL,
) ON [PRIMARY]
GO
INSERT INOUTData ( EmpID, CLockDate, [INTIME], [OUTTIME] ) VALUES
( 'E1', '28-Jan-2014', '07:50:00', '10:15:00' ),
( 'E1', '28-Jan-2014', '10:25:00', '12:15:00' ),
( 'E1', '28-Jan-2014', '17:15:00', '20:55:00' ),
( 'E2', '28-Jan-2014', '08:30:00', NULL ), -- First Session OUT punch missed
( 'E2', '28-Jan-2014', '16:15:00', '21:55:00' ),
( 'E3', '28-Jan-2014', NULL, '11:34:00' ), -- First Session IN punch missed
( 'E3', '28-Jan-2014', '16:15:00', '19:55:00' ),
( 'E3', '28-Jan-2014', '20:05:00', '21:55:00' ),
( 'E4', '28-Jan-2014', '08:30:00', '11:34:00' ),
( 'E4', '28-Jan-2014', NULL, '21:55:00' ), -- Second Session IN punch missed
( 'E5', '28-Jan-2014', '10:35:00', '13:44:00' ),
( 'E5', '28-Jan-2014', '18:55:00' , NULL ) -- Second Session OUT punch missed
ShiftDetails
:
Create Table ShiftDetails
(
ShiftId INT ,
ShiftName VARCHAR(20),
Session1StartTime TIme ,
Session1EndTime TIme ,
Session2StartTime TIme ,
Session2EndTime TIme
)
Insert into ShiftDetails values
(1,'Break Shift', '08:00:00', '12:00:00', '17:00:00', '21:00:00'),
(2,'Break Shift2', '10:00:00', '14:00:00', '19:00:00', '23:00:00')
我正在尝试将这两个表格结合起来,如下所示
EMpID ClockDate Session1StartTime Session1EndTime ActualSession1StartTime ActualSession1EndTime Session2StartTime Session2EndTime ActualSession2StartTime ActualSession2EndTime
E1 28-Jan-2014 08:00:00 12:00:00 07:50:00 12:15:00 17:00:00 21:00:00 17:15:00 20:55:00
E2 28-Jan-2014 08:00:00 12:00:00 08:30:00 NULL 17:00:00 21:00:00 16:15:00 21:55:00
E3 28-Jan-2014 08:00:00 12:00:00 NULL 11:34:00 17:00:00 21:00:00 16:15:00 21:55:00
E4 28-Jan-2014 08:00:00 12:00:00 08:30:00 11:34:00 17:00:00 21:00:00 NULL 21:55:00
E5 28-Jan-2014 10:00:00 14:00:00 10:35:00 14:44:00 19:00:00 23:00:00 18:55:00 NULL
请提出如何以有效方式实现的建议,现在我正在使用遍历每条记录并完成工作的存储过程。
有没有办法用Pivot或查询代替游标