We have a table used to assign trainers to planned client settings. In the table for each trainer there is an entry for each day of the year. (We can and sometimes do work weekends.) I create a search tool that allows our planners to look for a trainer who is available X the number of days between dates Y and Z.
Table mySchedule Trainer Date Dirty (Bit) ------------------------------------------------ Joe 06/01/2013 0 Jessica 06/01/2013 0 Alan 06/01/2013 0 Heather 06/01/2013 0 Joe 06/02/2013 1 Jessica 06/02/2013 1 Alan 06/02/2013 0 Heather 06/02/2013 0 Joe 06/03/2013 1 Jessica 06/03/2013 1 Alan 06/03/2013 1 Heather 06/03/2013 0
This is a simplified version of my table, covering four trainers over 3 days. If they have something planned, Dirty = 1. If they are free for the schedule, Dirty = 0.
What I would like to build is a query that allows the following:
Determine the start and end dates at which work should be performed.
Determine the number of consecutive days during which the trainer will be needed.
Return every trainer that matches the first date available over a period of time at least equal to the number of days requested.
Simple text example:
The client asks the trainer to be in place for two days at any time in June. The request should return:
Alan, 06/01/2013 Heather, 06/01/2013
If the client changed the request for three days in June, the request will return:
Heather, 06/01/2013
I searched for a few days and I found some things that seemed close, but in the end I could not get them to work. In most cases, the denial was in the form of an insanely long lead time. Here are some of them that seem promising and perhaps could be adapted by someone with stronger SQL-Fu than I am packaging:
source share