This is a follow up to my previous question:
First-In and Last-Out times for Night Shift Employees
I am a beginner ofin SQL. I tried this Queryquery and CTE But, but I cannot get my desire Outputdesired output:
Shift Table ###Shift Table
UserID ShiftName Start End
---------------------------------------------------
1019 Night Shift-1 18:30 03:30
UserID ShiftName Start End
---------------------------------------------------
1019 Night Shift-1 18:30 03:30
Employee Table ###Employee Table
UserID LogDate C1
---------------------------------------------------
1019 2016-03-01 05:17:03.000 Out
1019 2016-03-01 18:41:14.000 In
1019 2016-03-01 22:06:24.000 Out
1019 2016-03-01 22:34:03.000 In
1019 2016-03-02 01:32:33.000 Out
1019 2016-03-02 01:38:03.000 In
1019 2016-03-02 05:32:33.000 Out
UserID LogDate C1
---------------------------------------------------
1019 2016-03-01 05:17:03.000 Out
1019 2016-03-01 18:41:14.000 In
1019 2016-03-01 22:06:24.000 Out
1019 2016-03-01 22:34:03.000 In
1019 2016-03-02 01:32:33.000 Out
1019 2016-03-02 01:38:03.000 In
1019 2016-03-02 05:32:33.000 Out
Desired output Table1: ###Desired output Table1:
UserId Date LogIN LogOUT
-------------------------------------------------------------------
1019 2016-03-01 2016-03-01 18:41:14 2016-03-01 22:06:24
1019 2016-03-01 2016-03-01 22:34:03 2016-03-02 01:32:33
1019 2016-03-01 2016-03-02 01:38:03 2016-03-02 05:32:33
UserId Date LogIN LogOUT
-------------------------------------------------------------------
1019 2016-03-01 2016-03-01 18:41:14 2016-03-01 22:06:24
1019 2016-03-01 2016-03-01 22:34:03 2016-03-02 01:32:33
1019 2016-03-01 2016-03-02 01:38:03 2016-03-02 05:32:33
Desired output Table2: ###Desired output Table2:
UserId Date FirstIN LastOUT
-------------------------------------------------------------------
1019 2016-03-01 2016-03-01 18:41:14 2016-03-02 05:32:33
UserId Date FirstIN LastOUT
-------------------------------------------------------------------
1019 2016-03-01 2016-03-01 18:41:14 2016-03-02 05:32:33
Additional notes:
- There may be multiple IN and OUT for each employee.
- The shift times are 18:30 and 03:30.
- Employees may come maximum 2 hours earlier or leave 2 hours later.