2016-04-30 17 views
1

每天甲骨文基於時間戳的計算我有一個包含兩個時間戳記T1(活動廣場日期)T2(事件結束日期)和主鍵事件ID表。使用兩個datetime列

如果事件是開放的,然後T2時甚至被封閉在同一行會與事件關閉日期T2將得到更新。 例如,我想檢查在開放日期的每一天打開多少個問題(t1)01-apr-20162016年4月10日

我必須根據選定的日期範圍計算每天打開多少個事件。 比方說,如果事件ID 1已得到了開幕1-APR,結果被封閉10-APR,我計算的開通問題的數量每天在11-APR那麼就應該給我開數事件1從1月1日到10月4日。

表結構: -

================================================ 
    EVENTID   T1    T2 
================================================ 
     1   01-apr-2016  10-apr-2016 
     2   02-apr-2016  08-apr-2016 
     3   05-apr-2016  09-apr-2016 

預期輸出: -

============================================================================== 
     DATE   TOTAL_OPEN_EVENTS 
============================================================================== 
    01-apr-2016   1 
    02-apr-2016   2(1 issue open on 1st(not closed on 2nd) and 1 on 2nd) 
    03-apr-2016   2 
    04-apr-2016   2 
    05-apr-2016   3 
    06-apr-2016   3 
    07-apr-2016   3 
    08-apr-2016   2(1 issue got closed on 8th(which was opened on 2nd)) 
    09-apr-2016   2 
    10-apr-2016   0 

如何做這種計算的Oracle數據庫?

+0

請顯示示例表數據和預期輸出。 – OldProgrammer

+0

@OldProgrammer添加了表結構和預期輸出。 –

+0

不應該指望09年4月09日是1,而不是2?事件#3已關閉。 – OldProgrammer

回答

2

爲了生成結束報告,您需要爲您所需範圍內的每個日期生成一行。您可以使用日曆表(如果可用),或者我發現使用DUAL上的查詢使用CONNECT BY LEVEL < some_number可以很好地生成行。 (在這種情況下「some_number」將要報告的天數。) 從那裏,你只需要加入個別日期的日期範圍在你的事件表:

-- create table "events" table 
create table event_date_ranges 
as 
select 1 as event_id, TO_DATE('2016-APR-01', 'YYYY-MM-DD') as start_date, TO_DATE('2016-APR-10', 'YYYY-MON-DD') as end_date from dual 
union all 
select 2 as event_id, TO_DATE('2016-APR-02', 'YYYY-MM-DD') as start_date, TO_DATE('2016-APR-08', 'YYYY-MON-DD') as end_date from dual 
union all 
select 3 as event_id, TO_DATE('2016-APR-05', 'YYYY-MM-DD') as start_date, TO_DATE('2016-APR-09', 'YYYY-MON-DD') as end_date from dual 
; 


with 
date_range_qry as 
(-- one way to set the start and end dates for your report 
select TO_DATE('2016-APR-01', 'YYYY-MM-DD') as report_start_date 
     , TO_DATE('2016-APR-10', 'YYYY-MM-DD') as report_end_date 
    from dual 
) 
, dates_qry 
as 
(
-- generate a row for all dates between 2016-APR-01 and 2016-APR-10 
select report_start_date + ROWNUM - 1 as report_date 
from dual 
    cross join 
    date_range_qry drq 
connect by level <= (drq.report_end_date - drq.report_start_date + 1) 
) 
select dq.report_date, count(edr.event_id) as total_open_events 
    from dates_qry dq 
     left outer join 
     event_date_ranges edr 
      on dq.report_date >= edr.start_date 
      and dq.report_date < edr.end_date 
group by dq.report_date 
order by dq.report_date 

輸出:

REPORT_DATE  TOTAL_OPEN_EVENTS 
2016-APR-01  1 
2016-APR-02  2 
2016-APR-03  2 
2016-APR-04  2 
2016-APR-05  3 
2016-APR-06  3 
2016-APR-07  3 
2016-APR-08  2 
2016-APR-09  1 
2016-APR-10  0 
0

你可以試試這個:

create table events_log 
as 
select 1 as event_id, TO_DATE('01-04-2016', 'DD/MM/YYYY') as T1, TO_DATE('10-04-2016', 'DD/MM/YYYY') as T2 from dual 
union all 
select 2 as event_id, TO_DATE('02-04-2016', 'DD/MM/YYYY') as T1, TO_DATE('08-04-2016', 'DD/MM/YYYY') as T2 from dual 
union all 
select 3 as event_id, TO_DATE('05-04-2016', 'DD/MM/YYYY') as T1, TO_DATE('09-04-2016', 'DD/MM/YYYY') as T2 from dual 
; 
-------------- 

select v.REPORT_DATE, count(t.EVENT_ID) as open_event 
    from events_log t, 
     (select to_date('01/04/2016', 'DD/MM/YYYY') + ROWNUM - 1 as report_date 
      from dual 
     connect by level <= (to_date('11/04/2016', 'DD/MM/YYYY') - 
        to_date('01/04/2016', 'DD/MM/YYYY') + 1)) v 
where t.T1(+) <= v.report_date 
    and t.T2(+) >= v.report_date 
group by v.report_date 
order by v.report_date; 

輸出無線將是:

report_date open_event 
01/04/2016 1 
02/04/2016 2 
03/04/2016 2 
04/04/2016 2 
05/04/2016 3 
06/04/2016 3 
07/04/2016 3 
08/04/2016 3 
09/04/2016 2 
10/04/2016 1 
11/04/2016 0