2014-02-21 67 views
0

我們有一個外匯定價系統,我們需要表示在給定的一週中某一天+時間內活動的一組ECN /交易所。因此可以將開始時間,結束時間和ECN名稱表示爲一個行。組合主鍵將是開始時間+結束時間+ ECN。例如。 -MTWThF- 08:00倫敦,-MTWThF- 14:30紐約,EBS。現在不能有重疊或重複。人們可以編輯記錄並且約束應該保留。在關係數據模型中表示時間表/時間表

如何最好地在數據庫級別表示此?如果它有助於我們使用Oracle,但是Oracle不可知的解決方案是首選。

現在忽略時區&夏令時,使它無法避免重疊。

回答

1

我會給出兩個答案,首先考慮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 |