Qba
Qba

Reputation: 128

Get all dates for all date ranges in table using SQL Server

I have table dbo.WorkSchedules(Id, From, To) where I store date ranges for work schedules. I want to create a view that will have all dates for all rows of WorkSchedules. Thanks to this I have 1 view with all dates for all schedules.

On web I only found solutions for 1 row like 2 parameters start and end. My issue is different where I have multiple rows with start and end range.

Example:

WorkSchedules

Id | From       | To
---+------------+-----------
1  | 2018-01-01 | 2018-01-05 
2  | 2018-01-08 | 2018-01-12

Desired result

1 | 2018-01-01
2 | 2018-01-02
3 | 2018-01-03
4 | 2018-01-04
5 | 2018-01-05
6 | 2018-01-08
7 | 2018-01-09
8 | 2018-01-10
9 | 2018-01-11
10| 2018-01-12

Upvotes: 0

Views: 799

Answers (3)

Paul Maxwell
Paul Maxwell

Reputation: 35603

If you are regularly dealing with "jobs" and "schedules" then I propose that you need a permanent calendar table (a table where each row is a unique date). You can create rows for dates dynamically but why do this many times when you can do it once and just re-use?

A calendar table, even of several decades, isn't "big" and when indexed they can be very fast as well. You can also store information about holidays and/or fiscal periods etc.

There are many scripts available to produce these tables, here's an answer with 2 scripts on this site: https://stackoverflow.com/a/5635628/2067753

Assuming you use the second (more comprehensive) script, then you can exclude weekends, or other conditions such as holidays, from query results.

Once you have a permanent Calendar table this style of query may be used:

CREATE TABLE WorkSchedules(
   Id   INTEGER  NOT NULL PRIMARY KEY 
  ,[From] DATE  NOT NULL
  ,[To]   DATE  NOT NULL
);
INSERT INTO WorkSchedules(Id,[From],[To]) VALUES (1,'2018-01-01','2018-01-05');
INSERT INTO WorkSchedules(Id,[From],[To]) VALUES (2,'2018-01-12','2018-01-12');

with range as (
    select min(ws.[From]) as dt_from, max(ws.[To]) dt_to
    from WorkSchedules as ws
    )
select c.*
from calendar as c
inner join range on c.date between range.dt_from and range.dt_to
where c.KindOfDay = 'BANKDAY'
order by c.date

and the result looks like this (note: "News Years Day" has been excluded)

              Date           Year   Quarter   Month   Week   Day   DayOfYear   Weekday   Fiscal_Year   Fiscal_Quarter   Fiscal_Month   KindOfDay   Description  
 ---- --------------------- ------ --------- ------- ------ ----- ----------- --------- ------------- ---------------- -------------- ----------- ------------- 
   1   02.01.2018 00:00:00   2018         1       1      1     2           2         2          2018                1              1   BANKDAY     NULL         
   2   03.01.2018 00:00:00   2018         1       1      1     3           3         3          2018                1              1   BANKDAY     NULL         
   3   04.01.2018 00:00:00   2018         1       1      1     4           4         4          2018                1              1   BANKDAY     NULL         
   4   05.01.2018 00:00:00   2018         1       1      1     5           5         5          2018                1              1   BANKDAY     NULL         
   5   08.01.2018 00:00:00   2018         1       1      2     8           8         1          2018                1              1   BANKDAY     NULL         
   6   09.01.2018 00:00:00   2018         1       1      2     9           9         2          2018                1              1   BANKDAY     NULL         
   7   10.01.2018 00:00:00   2018         1       1      2    10          10         3          2018                1              1   BANKDAY     NULL         
   8   11.01.2018 00:00:00   2018         1       1      2    11          11         4          2018                1              1   BANKDAY     NULL         
   9   12.01.2018 00:00:00   2018         1       1      2    12          12         5          2018                1              1   BANKDAY     NULL         

Without the where clause the full range is:

              Date           Year   Quarter   Month   Week   Day   DayOfYear   Weekday   Fiscal_Year   Fiscal_Quarter   Fiscal_Month   KindOfDay    Description    
 ---- --------------------- ------ --------- ------- ------ ----- ----------- --------- ------------- ---------------- -------------- ----------- ---------------- 
   1   01.01.2018 00:00:00   2018         1       1      1     1           1         1          2018                1              1   HOLIDAY     New Year's Day  
   2   02.01.2018 00:00:00   2018         1       1      1     2           2         2          2018                1              1   BANKDAY     NULL            
   3   03.01.2018 00:00:00   2018         1       1      1     3           3         3          2018                1              1   BANKDAY     NULL            
   4   04.01.2018 00:00:00   2018         1       1      1     4           4         4          2018                1              1   BANKDAY     NULL            
   5   05.01.2018 00:00:00   2018         1       1      1     5           5         5          2018                1              1   BANKDAY     NULL            
   6   06.01.2018 00:00:00   2018         1       1      1     6           6         6          2018                1              1   SATURDAY    NULL            
   7   07.01.2018 00:00:00   2018         1       1      1     7           7         7          2018                1              1   SUNDAY      NULL            
   8   08.01.2018 00:00:00   2018         1       1      2     8           8         1          2018                1              1   BANKDAY     NULL            
   9   09.01.2018 00:00:00   2018         1       1      2     9           9         2          2018                1              1   BANKDAY     NULL            
  10   10.01.2018 00:00:00   2018         1       1      2    10          10         3          2018                1              1   BANKDAY     NULL            
  11   11.01.2018 00:00:00   2018         1       1      2    11          11         4          2018                1              1   BANKDAY     NULL            
  12   12.01.2018 00:00:00   2018         1       1      2    12          12         5          2018                1              1   BANKDAY     NULL            

and weekends and holidays may be excluded using the column KindOfDay

See this as a demonstration (with build of calendar table) here: http://rextester.com/CTSW63441

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1270201

You would use a recursive CTE:

with dates as (
      select from, to, from as date
      from WorkSchedules
      union all
      select from, to, dateadd(day, 1, date)
      from dates
      where date < to
     )
select row_number() over (order by date), date
from dates;

Note that from and to are reserved words in SQL. They are lousy names for identifiers. I have not escaped them because I assume they are not the actual names of the columns.

Upvotes: 1

NicVerAZ
NicVerAZ

Reputation: 407

Ok, I worked this out for you, thinking you mean that you meant 01/08/2018 as a From date in the second row.

/*WorkSchedules
Id|    From    |     To
1 | 2018-01-01 | 2018-01-05 
2 | 2018-01-08 | 2018-01-12
*/

--DROP TABLE #WorkSchedules;

CREATE TABLE #WorkSchedules (
    ID int,
    [DateFrom] DATE,
    [DateTo] DATE
    )

INSERT INTO #WorkSchedules
SELECT 1, '2018-01-01', '2018-01-05'
UNION
SELECT 2, '2018-01-08', '2018-01-12'

;WITH CTEDATELIMITS AS (
    SELECT [DateFrom], [DateTo]
    FROM #WorkSchedules
    )
,CTEDATES AS
(
SELECT [DateFrom] as [DateResult] FROM CTEDATELIMITS
UNION ALL
SELECT DATEADD(Day, 1, [DateResult]) FROM CTEDATES
JOIN CTEDATELIMITS ON CTEDATES.[DateResult] >= CTEDATELIMITS.[DateFrom]
AND CTEDATES.dateResult < CTEDATELIMITS.[DateTo]
)
SELECT [DateResult] FROM CTEDATES
ORDER BY [DateResult]

Upvotes: 1

Related Questions