0

我遇到了这样的问题:

从今天到圣诞节还有几个月?

SQL> SELECT MONTHS_BETWEEN (TO_DATE('2013/10/14', 'YYYY/MM/DD'),
  2  TO_DATE('2013/12/25', 'YYYY/MM/DD')) AS MONTHS_BETWEEN FROM DUAL;

MONTHS_BETWEEN
--------------
    -2.3548387

现在我有这个问题:

名为 Flag Football Game 和 Guest Speaker 的 EVENT 之间有多少个月?

我无法弄清楚的问题是相关日期在同一列中(FLAG FOOTBALL 为 1,GUEST_SPEAKER 为 2)。此外,它们来自一张桌子,而不是像“双重”这样的非实体。

也许有一种方法可以在选择语句中嵌套一个“WHERE”子句,例如:

Select TO_CHAR(EVENT_DATE(WHERE EVENT_NAME = UPPER(‘Flag FOOtball Game’) 
– TO_CHAR(EVENT_DATE(WHERE EVENT_NAME = UPPER(‘GUEST SPEAKER’)AS 
TIME FROM FOOTBALL_TO_SPEAKER;

这是事件表:

SQL> select * from event;

  EVENT_ID   CLASS_ID EVENT_NAME                     EVENT_DAT T RAIN_DATE
---------- ---------- ------------------------------ --------- - ---------
         1          9 ASTRO Physics STAR GAZING      09-NOV-99 O
         2         10 Flag FOOtball Game             19-JAN-99 O 20-JAN-99
         3          1 Guest SPEAKER                  01-DEC-99 I
         4          8 Barney ViDeo                   01-SEP-99 I
         5         10 PICKUP SOCCER GAME             09-JUL-99 I 10-JUL-99
         6          2 Guest Speaker                  20-NOV-99 I

我需要做什么?

4

3 回答 3

1

这里有一个方法,

select months_between(
            max(case when event_name = 'Flag FOOtball Game'
                      then event_dat
                end),
            max(case when event_name = 'Guest Speaker'
                      then event_dat
                end)
  )
from event
where event_name in ('Flag FOOtball Game','Guest Speaker');
于 2013-10-15T08:28:01.290 回答
0

对于第一部分,以下内容应该是正确的。

SELECT trunc(MONTHS_BETWEEN (TO_DATE('2013/10/14', 'YYYY/MM/DD'), TO_DATE('2013/12/25', 'YYYY/MM/DD')) )AS MONTHS_BETWEEN FROM DUAL;


select trunc(MONTHS_BETWEEN(fg,gs)) as "difference in month"
from
(
select 
                 max(case when event_name = 'Flag FOOtball Game'
                     then event_dat
                 end) as FG,
                 max(case when event_name = 'Guest Speaker'
                           then event_dat
                 end)  as GS
from event
where event_name in ('Flag FOOtball Game','Guest Speaker')   
) 
;
于 2013-10-15T08:37:31.700 回答
0

只需在需要条件的情况下自行加入此表,例如:

SELECT t1.EVENT_DAT - t2.EVENT_DAT FROM EVENT t1, EVENT t2
WHERE t1.EVENT_NAME = 'Flag FOOtball Game' AND t2.EVENT_NAME = 'Guest Speaker'
于 2013-10-15T08:25:47.060 回答