2016-06-07 18 views
1

使用下面的截圖中顯示的數據集的一個例子,如何返回的時間全部免費時段從預約設定

INT,DATETIME2,DATETIME2,詮釋

我怎樣才能回報大家的時間段免費的所述開始內是可用的,並停止其也比當它們不存儲在數據庫中作爲預約時隙持續時間等於參數或更長範圍

declare @startRange datetime2 
declare @endRange datetime2 
declare @slotDurationInMinutes int 

set @startRange = '2016-06-06T22:00:00.000Z' 
set @endRange = '2016-06-07T21:59:00.000Z' 
set @slotDurationInMinutes = 30 


-- from this appointment dataset how do i query for all the free periods which are as long or longer than the slotduration parameter 
-- these values are not stored in the table? 
select TSO_Table_ID, time_start, time_end, duration from Org_TSO_Table 

enter image description here

例如查詢,其中slotduration PARAM爲30分鐘將是預期的輸出:

free_from = 2016-06-06T22:00:00.000Z free_until = 2016-06-06T22 :00:30.000Z

(此記錄包含在搜索範圍開始值)

free_from = 2016-06-06T22:01:30 .000Z free_until = 2016-06-06T22:04:00.000Z

free_from = 2016-06-06T22:04:20.000Z free_until = 2016-06-06T22:10:00.000Z

free_from = 2016-06-06T22:11:00.000Z free_until = 2016-06-06T22:11:30.000Z

free_from = 2016-06-06T22:12:30.000Z free_until = 2016-06-07T21:59:00.000Z

(此記錄包含在搜索範圍結束值)

+2

什麼決定了空閒時間?問題中沒有足夠的信息來回答。請閱讀[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 –

+0

免費期是一個不被預約開始和停止佔用的時間段,希望能夠澄清事情。 – Dizzle

+0

你累了什麼? –

回答

2

很難說出下面的解決方案是否適合你,因爲有小數據樣本,但希望它能幫助你開始。

IF OBJECT_ID('tempdb..#sked') is not null 
DROP TABLE #sked 

IF OBJECT_ID('tempdb..#tmpResults') is not null 
DROP TABLE #tmpResults 

create table #sked(
    ID int, 
    time_start datetime2, 
    time_end datetime2, 
    duration int) 

insert into #sked (ID,time_start,time_end,duration) values 
(4,'2016-06-06 00:30:00','2016-06-06 01:30:00',3600000), 
(2,'2016-06-06 04:00:00','2016-06-06 04:20:00',1200000), 
(1,'2016-06-06 10:00:00','2016-06-06 11:00:00',3600000), 
(6,'2016-06-06 11:30:00','2016-06-06 12:30:00',3600000) 

declare @startRange datetime2 
declare @endRange datetime2 
declare @slotDurationInMinutes int 

set @startRange = '2016-06-05T00:00:00.000' 
set @endRange = '2016-06-07T21:59:00.000' 
set @slotDurationInMinutes = 30 

select 
    time_end as free_from, 
    isnull(lead(time_start) over (order by time_end),@endRange) as free_until 
into #tmpResults 
from 
    #sked 
where 
    time_end >= @startRange 
    and time_end <= @endRange 
    --and duration <= (@slotDurationInMinutes * 60000) --conversion to milliseconds 

union all 

select 
    case when @startRange < min(time_start) then @startRange end as free_from, 
    case when @startRange < min(time_start) then min(time_start) end as free_until 
from 
    #sked 
where 
    time_end >= @startRange 
    and time_end <= @endRange 
    --and duration <= (@slotDurationInMinutes * 60000) --conversion to milliseconds 
order by 
    free_from 

select 
    *, 
    DATEDIFF(minute,free_from,free_until) 
from 
    #tmpResults 
where 
    free_from is not null 
    and DATEDIFF(minute,free_from,free_until) >= @slotDurationInMinutes 
+0

嗨,感謝您花時間回覆,看起來有點接近, – Dizzle

+0

不用擔心。讓我知道如果我能改善它爲你工作。這實際上取決於您希望參數如何工作,以及是否存在多個計劃的衝突。即您的數據集似乎只適用於一個實體,但如果您正在查看多個實體(人員,調度塊等),則可能無法產生所需的結果。但是,如果這對你有效,並且是你的答案,那真棒。 – scsimon

+0

對不起,評論超時,因爲我打字: 嗨,感謝您抽出寶貴時間回覆,看起來有點接近,我得到以下輸出: 2016-06-06 01:30:00.0000000 \t 2016-06- 06 04:00:00.0000000 2016-06-06 04:20:00.0000000 \t 2016-06-06 10:00:00.0000000 2016-06-06 11:00:00.0000000 \t 2016-06-06 11:30:00.0000000 2016-06-06 12:30:00.0000000 \t 2016-06-07 21:59:00.0000000 但是它不太正確,例如,如果我將startRange更改爲startRange ='2016-06-05T00:00:00.000'和插槽分鐘到30我預計2016-06-06 00:00.000到2016-06-06 00:30.00的結果將返回 – Dizzle

相關問題