2014-03-26 169 views
4

我有一個計算併發作業花費時間的sql查詢。下面是該查詢:計算花費時間的總和

SELECT 
      user_concurrent_program_name, 
      DECODE (phase_code, 'C', 'Completed', phase_code) phase_code, 
      DECODE (status_code, 
        'D', 'Cancelled', 
        'E', 'Error', 
        'G', 'Warning', 
        'H', 'On Hold', 
        'T', 'Terminating', 
        'M', 'No Manager', 
        'X', 'Terminated', 
        'C', 'Normal', 
        status_code) 
       AS status_code, 
      TO_CHAR (
       to_date('2001,091,00:00:00' , 'YYYY,DDD,HH24:MI:SS') 
       + (ACTUAL_COMPLETION_DATE - ACTUAL_START_DATE), 
       'HH24:MI:SS') 
      AS GECEN_SURE 
     FROM apps.fnd_conc_req_summary_v 
     WHERE phase_code = 'C' AND status_code = 'C'; 

這裏是幾排,當我運行它:

Initial Mass Copy      Completed Normal 22:12:35 
Gather Schema Statistics    Completed Normal 06:13:35 
Request Set FAH-KPK Fislerini Olustur Completed Normal 04:36:36 
Request Set FAH-KPK Fislerini Olustur Completed Normal 04:15:18 
Request Set FAH-KPK Fislerini Olustur Completed Normal 04:13:45 
Request Set FAH-KPK Fislerini Olustur Completed Normal 04:10:51 

我想要的是得到的所花費的時間的總和。所以我的查詢結果將如下:

Initial Mass Copy       Completed Normal 22:12:35 
    Gather Schema Statistics    Completed Normal 06:13:35 
    Request Set FAH-KPK Fislerini Olustur Completed Normal 17:02:36 
+2

開始通過消除查詢的時間格式 - 只是做了一些。然後使用整個事件作爲內聯視圖,並總結秒數。然後將其格式化爲HH24:MI:ss – Randy

回答

2

你可以這樣做像這樣fiddle

CREATE TABLE test 
(
    A CHAR (1), 
    b VARCHAR2 (10) 
); 

INSERT INTO TEST 
    VALUES ('A', '04:00:01'); 

INSERT INTO TEST 
    VALUES ('A', '04:00:01'); 

INSERT INTO TEST 
    VALUES ('A', '13:02:01'); 

INSERT INTO TEST 
    VALUES ('A', '11:00:01'); 

INSERT INTO TEST 
    VALUES ('A', '02:59:59'); 

COMMIT; 

SELECT TO_CHAR (TRUNC (total/3600)) 
     || ':' 
     || TO_CHAR (TRUNC (ABS (MOD (total, 3600))/60), 'fm00') 
     || ':' 
     || TO_CHAR (MOD (total, 60), 'fm00') 
    FROM (SELECT SUM (
        TO_NUMBER (SUBSTR (b, 1, 2)) * 60 * 60 
        + TO_NUMBER (SUBSTR (b, 4, 2)) * 60 
        + TO_NUMBER (SUBSTR (b, 7, 2))) 
        TOTAL 
      FROM TEST); 
35:02:03 

PS:這可以包含在您的聚合部分。

因此,它應該是這樣的

WITH TEST 
    AS (SELECT user_concurrent_program_name, 
       DECODE (phase_code, 'C', 'Completed', phase_code) phase_code, 
       DECODE (status_code, 
         'D', 'Cancelled', 
         'E', 'Error', 
         'G', 'Warning', 
         'H', 'On Hold', 
         'T', 'Terminating', 
         'M', 'No Manager', 
         'X', 'Terminated', 
         'C', 'Normal', 
         status_code) 
        AS status_code, 
       TO_CHAR (
        TO_DATE ('2001,091,00:00:00', 'YYYY,DDD,HH24:MI:SS') 
        + (ACTUAL_COMPLETION_DATE - ACTUAL_START_DATE), 
        'HH24:MI:SS') 
        AS GECEN_SURE 
      FROM apps.fnd_conc_req_summary_v 
      WHERE phase_code = 'C' AND status_code = 'C') 
SELECT user_concurrent_program_name, 
     phase_code, 
     status_code, 
      TO_CHAR (TRUNC (GECEN_SURE/ 3600)) 
     || ':' 
     || TO_CHAR (TRUNC (ABS (MOD (GECEN_SURE, 3600))/60), 'fm00') 
     || ':' 
     || TO_CHAR (MOD (GECEN_SURE, 60), 'fm00') TOTAL 
    FROM ( SELECT user_concurrent_program_name, 
       phase_code, 
       status_code, 
       SUM (
         TO_NUMBER (SUBSTR (GECEN_SURE, 1, 2)) * 60 * 60 
        + TO_NUMBER (SUBSTR (GECEN_SURE, 4, 2)) * 60 
        + TO_NUMBER (SUBSTR (GECEN_SURE, 7, 2))) 
        GECEN_SURE 
      FROM TEST 
     GROUP BY user_concurrent_program_name, phase_code, status_code); 
+0

感謝您的回答。但是執行查詢需要太多時間。我們可以做些什麼來提高績效? – AloneInTheDark

+1

這在構建函數使用方面非常密集。最好保留此功能。我的意思是把它分成兩個查詢。您也可以期待將這些疑問整合到一起。 – SriniV

3

似乎你想總結時間間隔。如果你有開始和結束時間的日期,你可以將它們轉換爲時間戳,然後減去它們,給你間隔。棘手的部分是聚合間隔,你需要一個自定義的聚合函數。

綜上所述間隔,請參閱我以前的帖子here

一旦編譯,你可以簡單地做到以下幾點:

with d as (
    -- 1 day duration 
    select 'X' as typ, sysdate - 4 as start_dte, sysdate - 3 as end_dte from dual 
    union all 
    -- 1.5 days duration 
    select 'X' as typ, sysdate - 3 as start_dte, sysdate - 1.5 as end_dte from dual 
    union all 
    -- 0.5 days duration 
    select 'Y' as typ, sysdate - 1.5 as start_dte, sysdate - 1 as end_dte from dual 
    union all 
    -- 1 day duration 
    select 'Z' as typ, sysdate - 1 as start_dte, sysdate as end_dte from dual 
) 
-- subtract timestamp gives intervals 
select typ, sum_interval(cast(end_dte as timestamp) - cast(start_dte as timestamp)) as duration 
from d 
group by typ; 

輸出:

TYP DURATION 
X +02 12:00:00.000000 
Y +00 12:00:00.000000 
Z +01 00:00:00.000000 
相關問題