I have questions about sub query. I have table:
Emp_ID | Date_Entry | Clock_In | Clock_Out |
---|---|---|---|
E-0001 | 2024-02-01 | 2024-02-01 22:01:00 | NUll |
E-0001 | 2024-02-02 | NULL | 2024-02-02 06:01:05 |
E-0001 | 2024-02-03 | 2024-02-03 06:01:00 | 2024-02-03 14:00:20 |
E-0001 | 2024-02-04 | 2024-02-04 08:00:00 | 2024-02-04 17:01:05 |
how to query with mysql so when the clock_in >= 22:00:00 then the clock_out is select the next day clock_out like below?:
Emp_ID | Date_Entry | Clock_In | Clock_Out |
---|---|---|---|
E-0001 | 2024-02-01 | 2024-02-01 22:01:00 | 2024-02-02 06:01:05 |
E-0001 | 2024-02-02 | NULL | 2024-02-02 06:01:05 |
E-0001 | 2024-02-03 | 2024-02-03 06:01:00 | 2024-02-03 14:00:20 |
E-0001 | 2024-02-04 | 2024-02-04 08:00:00 | 2024-02-04 17:01:05 |
any help will be appreciated. thank you in advance!
2
Answers
If you’re working with MySQL 8, the window function
LEAD()
combined withCASE
clause allows you to retrieve the next Clock_Out for cases where clock_in is 22:00:00 or later :Demo here