//night shift employee
id from_time to_time task
1 21:00:00 22:00:00 - Cleaning(some task)
1 22:00:00 23:30:00 - Fumigation(can be some other task also)
1 4:00:00 7:00:00 - Disinfection
1 2:00:00 4:00:00 - Break
1 23:30:00 2:00:00 - Fogging
//day shift employee
2 09:00:00 10:00:00 - Cleaning(some task)
2 16:00:00 18:30:00 - Disinfection
2 11:30:00 14:00:00 - Fumigation(can be some other task also)
2 14:00:00 16:00:00 - Fogging
2 10:00:00 11:30:00 - Break
I need to sort the tasks based from_time in ascending order
SELECT * FROM testing WHERE emp_id='2' ORDER BY from_time ASC
The above query works fine for day shift employee but not sorting for night shift
SELECT * FROM testing WHERE emp_id='1'
ORDER BY
CASE
WHEN CAST(from_time AS TIME) > '12:00:00' THEN 1
ELSE 2
END,
CAST(from_time AS TIME) ASC;
The above query works fine for night shift emplyee but not sorting for day shift
How can I solve this issue?
I need one query which can sort both night shift and day shift employees tasks based on from_time.
2
Answers
You could check the time difference between the minimum and maximum
from_time
per employee shift:Outputs:
Here’s a db<>fiddle.
It would be better to link to the start of the shift in some way, but this may work depending on your shift patterns.
Add date to your FROM_TIME and correct it after midnight as next day. Your Order By could have this code:
This adds from_time to current date and adds 1 day for ID = 1 if from time hour is less than 12. If your night shift is from 19:00 today untill 07:00 tommorrow then put < 7 instead of < 12 in case expression’s condition. Order by this is order by DATETIME.
Sample data, SQL code and result are below:
Without SHIFT_ID in Order By clause the result should be …