We help IT Professionals succeed at work.
Get Started

SQL Recursion schedule

151 Views
Last Modified: 2021-04-21
I have a table of dates here are my column names

sample
WeekID      StartDate          EndDate
1                2017-01-06     2017-01-13 
2                2017-01-13     2017-01-20 

Open in new window


My second Table OnCallSupport here is a sample
Team            SequenceID        TechName
Team1                  1                         Joe  
Team1                  2                         jr   
Team1                  3                         Leo
Team1                  4                          Julian
Team2                  1                         Sam
Team2                  2                         David   
Team2                  3                         Cary
Team2                  4                          Stepnahie
Team2                  5                          Nicole
Team3                  1                         Noel  
Team3                  2                         Candice   
Team3                  3                         Jose
Team3                  4                         Rafael
Team4                  1                         Jessica  
Team4                  2                         Alice   
Team4                  3                         Yanet
Team4                  4                         Richard
 

Open in new window


I need 1 person from each team to be on call. How could you join these tables to create a schedule out for a year.

Here is the code to create the Dates view

WITH cteIDate AS (
SELECT  1 AS WeekID,
dateadd(day, (@@datefirst - datepart(weekday, dateadd(year, YEAR(Getdate()) - 1900, 0)) + (10 - @@datefirst) * 2) % 7, dateadd(year, YEAR(Getdate()) - 1900, 0)   ) as  StartDate
, dateadd(day, (@@datefirst - datepart(weekday, dateadd(year, YEAR(Getdate()) - 1900, 0)) + (10 - @@datefirst) * 2) % 7, dateadd(year, YEAR(Getdate()) - 1900, 0)   ) + 7  as EndDate
UNION ALL
SELECT WeekID + 1 AS ID,
dateadd(day , 7, StartDate) AS StartDate, dateadd(day , 7, EndDate) AS EndDate
FROM cteIDate
WHERE dateadd(day , 7, EndDate) < convert(varchar(4),Year(getdate())+1)+'-12-01'
)


SELECT * from cteIDate ci

Open in new window

Comment
Watch Question
Data Warehouse / Database Architect
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 13 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE