下一个5个可用日期

时间:2015-11-04 15:59:52

标签: sql sql-server

我想知道是否有人可以告诉我如何使用仅存储周末日期和银行假日日期的表来获得接下来的5个可用日期..所以它必须选择接下来的5天不与任何日期发生碰撞在表中。

我希望从这个日期列表中看到以下结果:

07/11/2015 (Saturday)
08/11/2015 (Sunday)
09/11/2015 (Holiday)
14/11/2015 (Saturday)
15/11/2015 (Sunday)

结果:

05/11/2015 (Thursday)
06/11/2015 (Friday)
10/11/2015 (Tuesday)
11/11/2015 (Wednesday)
12/11/2015 (Thursday)`

2 个答案:

答案 0 :(得分:1)

基于有限的信息,这是一个快速的黑客:

with offsets(n) as (
    select  1 union all
    select  2 union all
    select  3 union all
    select  4 union all
    select  5 union all
    select  6 union all
    select  7 union all
    select  8 union all
    select  9 union all
    select 10 union all
    select 11
)
select top 5 dateadd(dd, n, cast(getdate() as date)) as dt from offsets
where dateadd(dd, n, cast(getdate() as date) not in (
    select dt from <exclude_dates>
)
order by dt

答案 1 :(得分:1)

可能的解决方案是创建一年中所有可能日期的表格。

select top 5 date
from possible_dates
where date not in
   (select date from unavailable_dates)
and date > [insert startdate here]
order by date