2015-11-04 61 views
-2

我想知道有沒有人能告訴我如何使用僅存儲週末日期和銀行假期日期的表格來獲取接下來的5個可用日期..所以它必須選擇接下來的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)` 
+0

什麼嘗試一些和後一個問題 – ehh

+0

不是真的知道從哪裏開始.. –

+0

怎麼樣記錄和預期的結果ATLEAST –

回答

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 
+0

有了聖誕節,你可能只會得到4個結果(週末2天+聖誕節2天),與11更好的結合。 – Alfons

+0

謝謝Shawnt00 ..我認爲這正是我需要的..我在工作日看到類似的東西不包括週末),但不確定我是否可以使用它..我想我應該發佈它像頂部的人建議反正..非常感謝你,我相信這會幫助我。 –

+0

@Alfons是的,我在想10可能是,但這是一個很好的方案。 – shawnt00

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