计算花费时间的总和

时间:2014-03-26 12:32:29

标签: sql oracle sum

我有一个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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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);