mardi 6 janvier 2015

Generating shift pattern in SQL

I don't know even how to start addressing this issue, any assistance will be appreciated:


My goal is to generate a table (dynamically) that return dates that are working shift.


Settings:



  • I have a pattern as follow 7 days on duty 7 days off duty and it goes on...

  • I can determine the start date that the shift start

  • I can determine the pattern days (7 days - meaning 7 days on 7 days off)

  • i can determine the end date for calculating the patern

  • I want to calculate and create the following table


For example: Pattern days: 7 Start date: 01/01/2015 Pattern end date: 12/31/2015



ID StartshiftDate EndShiftDate OnDuty
-------------------------------------------------------------
1 01/01/2015 01/07/2015 On Duty
2 01/08/2015 01/14/2015 Off Duty
3 01/15/2015 01/21/2015 On Duty


I know that i need to create CTE that start from the start date, i need to add 7 days for each date. but i don't know how to determine if the range of dates is on duty or off duty.


And how i create the the loop for creating the row till the pattern end date?


Any help will be appreciated


Aucun commentaire:

Enregistrer un commentaire