简体   繁体   中英

SQL: Get all dates between two dates with shift

I have a table that content the column "startbooking" and "endbooking" I have other that content the shift:

id |start  |end
M  | 06:00 |14:00
T  | 14:00 |20:00
N  | 20:00 |06:00

so, i need to get all dates between startbooking and endbooking with the shifts

example:

startbooking: 01/05/2015 12:00  endbooking: 02/05/2015 16:00

result:

01/05/2015 |M
01/05/2015 |T
01/05/2015 |N
02/05/2015 |M
02/05/2015 |T

Perhaps you will need recursive cte with cross join :

with t as (
     select startdt, enddt
     from table
     union all
     select dateadd(day, 1, startdt), enddt
     from t
     where startdt < enddt
)

select t.startdt, sft.id
from t cross join (select distinct id from shifttable) sft
option (maxrecursion 0);

Here is a Demo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM