我會給出兩個答案,首先考慮RFC 5545的重複規則格式,並在應用層進行驗證。
由於這可能不是您正在尋找的答案,因此我還建議查看本週的任何一點作爲從周開始的偏移量。
然後,您可以建立一個轉換列表,這些轉換列表定義了交易所打開或關閉的周內的偏移量。所需的唯一約束是驗證偏移量是否有效(少於7天),並且開放到閉合的轉換隻是緊跟在後面的轉換(反之亦然)。
我沒有一個Oracle對話框打開在打轉轉,但這裏是在T-SQL的例子:
create table #sched (exchangeID int, weekOffset int, isOpen bit)
insert into #sched
values (1, 400, 1),
(1, 800, 0),
(1, 1200, 1),
(1, 1700, 0);
-- constraints are that isOpen cannot be repeated. 1->0->1 is valid, 1->1->0 is not
declare @coffset int,
@weekStart datetime;
set @weekstart = dateadd(week, datediff(week, 0, getdate()), 0);
set @coffset = datediff(minute, @weekStart, getdate());
with trans (uniqid, exch, offset, isopen)
as
(
select ROW_NUMBER() OVER (ORDER BY weekoffset), *
from (
SELECT exchangeID, weekoffset - 7*24*60 as weekoffset, isopen from #sched
UNION
select exchangeID, weekoffset, isopen from #sched
UNION
select exchangeID, weekoffset + 7*24*60, isopen from #sched
) as inr
)
select *,
CASE pt.IsOpen
WHEN 1 THEN 'Exchange is now open and will close at ' + CAST(DATEADD(minute, nt.offset, @weekstart) as varchar(20))
ELSE 'Exchange is now closed and will open at ' + CAST(DATEADD(minute, nt.offset, @weekstart) as varchar(20)) END
from (
SELECT MIN(uniqid) as nextTransition
FROM trans
WHERE offset > @coffset
) as n
cross join (
SELECT MAX(uniqid) as prevTransition
FROM trans
WHERE offset <= @coffset
) as p
inner join trans as nt on nt.uniqid = nextTransition
inner join trans as pt on pt.uniqid = prevTransition
drop table #sched
Results上sqlfiddle:
| NEXTTRANSITION | PREVTRANSITION | UNIQID | EXCH | OFFSET | ISOPEN | COLUMN_10 |
|----------------|----------------|--------|------|--------|--------|-------------------------------------------------------------|
| 9 | 8 | 9 | 1 | 10480 | 1 | Exchange is now closed and will open at Feb 24 2014 6:40AM |