I have attendance table like this:
Employee | Date_Time | Status |
---|---|---|
std001 | 2024-02-01 22:01:00 | IN |
std001 | 2024-02-02 06:00:05 | OUT |
std001 | 2024-02-03 07:59:00 | IN |
std001 | 2024-02-03 17:00:00 | OUT |
std001 | 2024-02-04 06:00:00 | IN |
std001 | 2024-02-04 14:00:00 | OUT |
I want to select the table above to get result like this below:
Employee | Date | DateTime_In | DateTime_Out |
---|---|---|---|
std001 | 2024-02-01 | 2024-02-01 22:01:00 | 2024-02-02 06:00:05 |
std001 | 2024-02-02 | NULL | 2024-02-02 06:00:05 |
std001 | 2024-02-03 | 2024-02-03 07:59:00 | 2024-02-03 17:00:00 |
std001 | 2024-02-04 | 2024-02-04 06:00:00 | 2024-02-04 14:00:00 |
the "DateTime_In" is from the "Date_Time" with status "IN", and if the "DateTime_In" is < 22:00:00 then the "DateTime_Out" is the "Date_Time" with status "OUT" from same date. but if the "DateTime_In" >= 22:00:00 then the "DateTime_Out" is the "Date_Time" with Status "OUT" from the next day.
can anyone help me with the query in mysql server? thank you!
2
Answers
Please:
This query should work exactly as you need it:
We need first get Date_Time_In and Date_Time_Out using the conditional aggregation, and then, apply the
LEAD()
function whenever Date_Time_In > 22:00 :Demo here