關於時間跨度比較的OVERLAPS函式

guoge發表於2008-09-12
Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4  

我曾經在我的部落格http://hitc.blogdriver.com/hitc/1288452.html 裡介紹瞭如何使用工作區管理器(workspace)來管理時態。但必須首先配置好工作區管理器。 由於blogdriver 空間有問題,所以計劃把部落格遷到ITPUB

今天檢視抓蝦訂閱的部落格,看到這麼Undocumented OVERLAPS Function, Don't Use it Yet ,文中提到OVERLAPS函式,例如:

 

SQL>  SELECT *

  2     FROM dual

  3    WHERE (add_months(sysdate, -6), DATE '2008-08-08')

  4          OVERLAPS

  5          (sysdate - 180, interval '2' YEAR);

D

-

X

 

Mimer SQL Reference Manual: 中對OVERLAPS進行了一些解釋:

 

 

    The OVERLAPS predicate tests whether two “events” cover a common point in time or not, and has the form.:

 

    (expression, expression) OVERLAPS (expression, expression)

 

    Each of the two “events” specified on either side of the OVERLAPS keyword is a period of time between two specified points on the timeline. The two points can be specified as a pair of datetime values or as one datetime value and an INTERVAL offset.

 

    Each “event” is defined by a two expressions constituting a row value expression having two columns.

 

    The first column in each row value expression must be a DATE, TIME or TIMESTAMP and the value in the first column of the first “event” must be comparable.

 

    The second column in each row value expression may be either a DATE, TIME or TIMESTAMP that is comparable with the value in the first column or an INTERVAL with a precision that allows it to be added to the value in the first column.

 

    The value in the first column of each row value expression defines one of the points on the timeline for the event.

 

    If the value in the second column of the row value expression is a datetime, it defines the other point on the timeline for the event.

 

    If the value in the second column of the row value expression is an INTERVAL, the other point on the timeline for the event is defined by adding the values in the two column of the row value to expression together.

 

    The NULL value is assumed to be a point that is infinitely late in time.

 

    Either of the two points may be the earlier point in time.

 

    If the value in the first column of the row value expression is the NULL value, then this is assumed to be the later point in time.

 

從這裡,我們可以看出OVERLAPS有兩種用法:

除了本文開始提到的例子外,還有:

SQL> select * from dual

  2   where          (date '2007-01-01', interval '5' year)

  3         overlaps (date '2005-01-01', interval '10' year);

 

D

-

X

 

比較的只能是日期,而不是數字,例如:

SQL> select * from dual

  2       where          (1,5)

  3             overlaps (3,8) ;

     where          (1,5)

                     *

ERROR at line 2:

ORA-00932: inconsistent datatypes

 

由於這個函式是ORACLE的未公開的,所以不宜在產品系統中使用。一個替代方法:

where (s2 < e1 and e2 > s1) or (s1 < e2 and e1 > s2)

 

 

這個函式最早出現在David Aldridge’s  


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9036/viewspace-446865/,如需轉載,請註明出處,否則將追究法律責任。

相關文章