Hello
declare @t table ( emp_code varchar(6), trans_date datetime, status varchar(2), time_portion varchar(8))
ideally when an employees comes in office he should punched in (status=01)
and when he goes out of office he should punch out (status=02)
but in practice there are a lot of irregularities.
- The employee punches in and does not punch out.
- Pnch out and didnt punched in.
- Repeated punch ins.
- Repeated punch outs.
We need show in and out in the same line within date group.
Sample data
insert @t (emp_code, trans_date, status, time_portion)
values('004070','2023-10-02','01','19:51:59'),
('004070', '2023-10-02', '02', '19:52:01'),
('004070', '2023-10-03', '02', '09:09:57'),
('004070', '2023-10-03', '02', '19:49:26'),
('004070', '2023-10-03', '01', '19:49:47'),
('004070', '2023-10-04', '02', '09:05:55'),
('004070', '2023-10-04', '01', '20:22:52'),
('004070', '2023-10-04', '02', '20:22:54'),
('004070', '2023-10-04', '06', '20:22:58'),
('004070', '2023-10-04', '02', '20:23:00'),
('004070', '2023-10-05', '02', '09:18:59'),
('004070', '2023-10-05', '01', '09:19:02'),
('004070', '2023-10-05', '02', '14:44:44')
expected result
trans_date timein timeout
2023-10-02, 19:51:59 19:52:01
2023-10-03, NULL 19:09:57
2023-10-03, NULL 19:49:26
2023-10-03, 19:49:47 NULL
2023-10-04, NULL 09:05:55
2023-10-04, 20:22:52 20:22:54
2023-10-04, NULL 20:23:00
2023-10-05, NULL 09:18:59
2023-10-05, 09:19:02 14:44:44
There is one more problem. We have sql server 2008 and cannot use lead or lag command.