2013-10-15 65 views
0

我有這樣一個問題:ORACLE SQL - 事件之間的時間?

從今天到聖誕節有幾個月?

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 

現在我有這樣的問題:

多少個月是在兩者之間評爲旗橄欖球比賽和演講嘉賓的事件?

我不明白的問題是相關日期在同一列(1爲標誌足球,2爲GUEST_SPEAKER)。此外,他們來自一張桌子,而不是像「雙重」這樣的非實體。

或許有辦法巢「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 

什麼我需要做的?

回答

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' 
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'); 
+0

行錯誤6: ORA-00904: 「EVENT_DATE」:無效的標識符 –

+0

根據您的表中的名稱更改的列名。在你的表中,這是EVENT_DAT – Noel

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') 
) 
; 
+0

我在這裏收到一個錯誤:錯誤在第1行: ORA-00909:無效的參數數 –

+0

更新query.please再試一次 – Shann

+0

這應該顯示所需的輸出 – Shann

相關問題