I have a table with userID, clockin(1)/Clockout(0), dateTime for few employees. in/out shows when someone is on (1) or off(0) clock.
- Job shift can span across midnight, as in punch in before midnight, and punchout in the morning. (Eg: Date 21st in table)
- Shift can last more than 24 hours (hypothetically) (Eg : Date 24)
- Punchin and punchout can happen multiple times within 24 hrs as well(Eg : Date 22).
I would like to get the sum of hours worked per day for any given user_id but within midnight to midnight, even though the shift might span across midnight. Timestamps are shown all with :30:00 for clarity. Only one user_id is shown, but this table can have info from multiple users, so user_id will be used in the where clause.
[id] [User_id] [Date_time] [in_out]
1 1 2022-08-20 09:30:00 1
2 1 2022-08-20 21:30:00 0
3 1 2022-08-21 20:30:00 1
4 1 2022-08-22 08:30:00 0
5 1 2022-08-22 09:30:00 1
6 1 2022-08-22 14:30:00 0
7 1 2022-08-23 12:30:00 1
8 1 2022-08-25 09:30:00 0
9 1 2022-08-25 12:30:00 1
So The desired query result would be something like below. The formatting does not matter. Total time per day in seconds or minutes or anything will work.
[Day] [hours_worked]
2022-08-20 12:00:00
2022-08-21 03:30:00
2022-08-22 13:00:00
2022-08-23 11:30:00
2022-08-24 24:00:00
2022-08-25 09:30:00
I started with the code from Get total hours worked in a day mysql This works well when punch-in happens before punch outs in a day, and does not handle midnights. Just trying to adapt to the specific case. Any help much appreciated.