我有这个查询,它返回每天员工的出勤时间。
SELECT
ROW_NUMBER() OVER (ORDER BY CAST(COALESCE(AttendanceTimeIn, AttendanceTimeOut) AS DATE)) RN, FirstName+' '+LastName EmployeeName,
CAST(COALESCE(AttendanceTimeIn, AttendanceTimeOut) AS DATE) DateOn,
CAST(COALESCE(AttendanceTimeIn, AttendanceTimeOut) AS TIME) AttendanceTime,
CASE WHEN AttendanceTimeIn IS NOT NULL THEN 'Time In' WHEN AttendanceTimeOut IS NOT NULL THEN 'Time Out' END Status
FROM HRM.tbl_EmployeeAttendance
INNER JOIN HRM.tbl_EmployeeInfo ON tbl_EmployeeAttendance.EmpCode = tbl_EmployeeInfo.ID
我想每天返回相等的行数,现在它正在返回以下行。
1 Yousuf Abdullah 2019-03-01 08:31:37.0000000 Time In
2 Yousuf Abdullah 2019-03-02 06:27:37.0000000 Time Out
3 Yousuf Abdullah 2019-03-02 13:12:12.0000000 Time In
4 Yousuf Abdullah 2019-03-03 13:12:12.0000000 Time In
我希望结果集是这样的。
1 Yousuf Abdullah 2019-03-01 08:31:37.0000000 Time In1
2 Yousuf Abdullah 2019-03-01 NULL Time Out1
3 Yousuf Abdullah 2019-03-01 NULL Time In2
4 Yousuf Abdullah 2019-03-01 NULL Time Out2
1 Yousuf Abdullah 2019-03-02 NULL Time In1
2 Yousuf Abdullah 2019-03-02 06:27:37.0000000 Time Out1
3 Yousuf Abdullah 2019-03-02 13:12:12.0000000 Time In2
4 Yousuf Abdullah 2019-03-02 NULL Time Out2
1 Yousuf Abdullah 2019-03-03 13:12:12.0000000 Time In1
2 Yousuf Abdullah 2019-03-03 NULL Time Out1
3 Yousuf Abdullah 2019-03-03 NULL Time In2
4 Yousuf Abdullah 2019-03-03 NULL Time Out2
我实际上想要返回的是4行或更多行,如果可能的话,每天最多返回10次。如果当天的进出时间存在,它将填满该时段,否则它将返回NULL或其他内容。
这是示例数据
CREATE TABLE HRM.tbl_EmployeeInfo
(
ID INT PRIMARY KEY NOT NULL,
FirstName VARCHAR(25),
LastName VARCHAR(25)
);
CREATE TABLE HRM.tbl_EmployeeAttendance
(
ID INT PRIMARY KEY NOT NULL,
EmpCode INT,
AttendanceTimeIn datetime,
AttendanceTimeOut datetime
);
INSERT [HRM].[tbl_EmployeeAttendance] VALUES (1, 453, CAST(N'2019-03-01T08:31:37.000' AS DateTime), NULL);
INSERT [HRM].[tbl_EmployeeAttendance] VALUES (2, 453, CAST(N'2019-03-02T06:27:37.000' AS DateTime), NULL);
INSERT [HRM].[tbl_EmployeeAttendance] VALUES (3, 453, CAST(N'2019-03-02T13:12:12.000' AS DateTime), CAST(N'2019-03-02T14:15:16.000' AS DateTime));
INSERT [HRM].[tbl_EmployeeAttendance] VALUES (4, 453, CAST(N'2019-03-03T13:12:12.000' AS DateTime), CAST(N'2019-03-03T14:15:16.000' AS DateTime));