I'm creating a small app to register visits and I've got stuck on finding free time windows between visits.
There are two tables, this is a simplified structure:
working_hours
- start_time
- end_timevisit
- start_time
- end_time
- visit_status
visits_status with value "2" are cancelled visits so we do not include them
Now small example:
Employee has his own working hours with breaks included, for example:
| start_time | end_time |
| 2018-12-29 08:00:00 | 2018-12-29 12:00:00 |
| 2018-12-29 12:30:00 | 2018-12-29 16:00:00 |
There are already visits registered in app, they may have different durations. Visit times have got included break so next visit can start right after . Let's say we have visits like those:
| start_time | end_time | visit_status |
| 2018-12-29 08:00:00 | 2018-12-29 08:30:00 | 1 |
| 2018-12-29 09:00:00 | 2018-12-29 10:00:00 | 1 |
| 2018-12-29 10:00:00 | 2018-12-29 10:40:00 | 1 |
| 2018-12-29 10:40:00 | 2018-12-29 11:10:00 | 2 |
| 2018-12-29 11:10:00 | 2018-12-29 11:40:00 | 0 |
| 2018-12-29 12:30:00 | 2018-12-29 13:00:00 | 0 |
| 2018-12-29 13:00:00 | 2018-12-29 14:00:00 | 0 |
| 2018-12-29 15:30:00 | 2018-12-29 16:00:00 | 0 |
My goal is to create query that will show me available start times for thirty minutes visit including working hours, In this specified case the result should be this times:
- 8:30
- 10:40
- 14:00
- 14:30
- 15:00