We have a table used for assigning trainers to planned customer installations. In the table, there is a record for each trainer, for each day of the year. (We can, and sometimes do, work weekends.) I am building a search tool that allows our schedulers to search for a trainer that is available X 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 scheduled, Dirty = 1. If they are free to schedule, Dirty = 0.
What I would like to build is a query that allows for the following:
Define a Beginning and Ending Date that the work needs to occur.
Define the number of consecutive days that the trainer will be required.
Return each Trainer that matches, along with the first date they are available for a period of time at least equal to the number of days being requested.
Plain text example:
The customer asks for a trainer to be onsite for two days anytime in June. The query should return:
Alan, 06/01/2013
Heather, 06/01/2013
If the customer changed the request to three days in June, the query would return:
Heather, 06/01/2013
I've been searching for a few days now, and I've found some things that seemed close, but ultimately, I couldn't get them to work. In most cases, the failure has been in the form of insanely long execution times. Here are a few that seemed promising, and perhaps can be adapted by someone with stronger SQL-Fu than I am packing: