I am working in SQL Server 2012. I have 3 tables. The first is a "schedule" table. Its structure is like:
CREATE TABLE schedule
(
JobID int
,BeginDate date
,EndDate date
)
Some sample data is:
INSERT INTO schedule
SELECT 1, '2017-01-01', '2017-07-31' UNION ALL
SELECT 2, '2017-02-01', '2017-06-30'
The second is a "frequency" table. Its structure is like:
CREATE TABLE frequency
(
JobID int
,RunDay varchar(9)
)
Some sample data is:
INSERT INTO frequency
SELECT 1, 'Sunday' UNION ALL
SELECT 1, 'Monday' UNION ALL
SELECT 1, 'Tuesday' UNION ALL
SELECT 1, 'Wednesday' UNION ALL
SELECT 1, 'Thursday' UNION ALL
SELECT 1, 'Friday' UNION ALL
SELECT 1, 'Saturday' UNION ALL
SELECT 2, 'Wednesday'
The third is a "calendar" table. Its structure is like:
CREATE TABLE calendar
(
CalendarFullDate date
,DayName varchar(9)
)
My goal is to "unpivot" the schedule table so that I create a row for each date spanning the date range in BeginDate and EndDate for each JobID. The rows must match the days in the frequency table per JobID.
Up until now, the frequencies of dates for each job are either daily or weekly. For this, I use the following SQL to generate my desired table:
SELECT
s.JobID
,c.CalendarFullDate
FROM
schedule AS s
INNER JOIN
calendar AS c
ON
c.CalendarFullDate BETWEEN s.BeginDate AND s.EndDate
INNER JOIN
frequency AS f
ON
f.JobID = s.JobID
AND f.RunDay = c.DayName
This doesn't work for frequencies that are higher than weekly (e.g., bi-weekly). To do so, I know that my frequency table would need to change structure. In particular, I would have to add a column that gives the frequency (e.g., daily, weekly, bi-weekly). And, I'm betting that I will need to add a week number column to the calendar table as well.
How can I generate my desired table to accommodate at least bi-weekly frequencies (if not higher frequencies)? For example, if JobID = 3 is a bi-weekly job that runs on Wednesday, and it's bound by BeginDate = '2017-06-01' and EndDate = '2017-07-31', then, for this job, I would expect the following in the result:
JobID Date
3 2017-06-07
3 2017-06-21
3 2017-07-05
3 2017-07-19