Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the following works:</p> <pre><code>select t.* from (select t.*, row_number() over (partition by r.requestid, IsWorkDay order by seqdate) as WorkDayNum from (select r.requestid, r.TransactionDate, (case when critical = 1 then 5 else 10 end) as DaysToRespond, dateadd(day, days.seqnum - 1, r.TransactionDate) as seqdate, (case when h.date is null then 1 else 0 end) as IsWorkDay from requests r cross join (select top 20 ROW_NUMBER() over (order by (select NULL)) as seqnum from information_schema.columns ) days left outer join holidays h on dateadd(day, days.seqnum - 1, r.TransactionDate) = h.date ) t ) t where WorkDayNum = DaysToRespond and IsWorkDay = 1 </code></pre> <p>This is untested, but here is the idea.</p> <p>Well, I have tested this, and it appears to return the right result in this case:</p> <pre><code>with holidays as ( select CAST('2012-01-01' as date) as date union all select CAST('2012-01-05' as date) as date union all select CAST('2012-01-06' as date) as date union all select CAST('2012-01-12' as date) as date union all select CAST('2012-01-13' as date) as date union all select CAST('2012-01-19' as date) as date union all select CAST('2012-01-20' as date) as date ), requests as ( select 1 as requestId, CAST('2012-01-02' as DATE) as TransactionDate, 1 as Critical ) select t.* from (select t.*, row_number() over (partition by t.requestid, IsWorkDay order by seqdate) as WorkDayNum from (select r.requestid, r.TransactionDate, (case when critical = 1 then 5 else 10 end) as DaysToRespond, dateadd(day, days.seqnum - 1, r.TransactionDate) as seqdate, (case when h.date is null then 1 else 0 end) as IsWorkDay from requests r cross join (select top 20 ROW_NUMBER() over (order by (select NULL)) as seqnum from INFORMATION_SCHEMA.columns ) days left outer join holidays h on dateadd(day, days.seqnum - 1, r.TransactionDate) = h.date ) t ) t where WorkDayNum = DaysToRespond+1 and IsWorkDay = 1 </code></pre> <p>This query creates a sequence of 20 days following the transaction date (Is 20 enough?). It then calculates the date for these days and compares the date to the holiday table.</p> <p>To count the number of days is uses <code>row_number()</code> partitioning by the request and workdays versus nonworkdays. The row to choose is the one that is a workday and the number of days after the transaction date. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload