【问题标题】:Calculate average/min/max/total in calculated values在计算值中计算平均值/最小值/最大值/总计
【发布时间】:2021-06-07 20:21:25
【问题描述】:

编辑:添加 PL/SQL 脚本:

我正在运行一个脚本来计算不同表中两列之间的时间差。它主要计算 CA 生成证书所花费的时间。 我想运行平均/最小/最大和过去 10 分钟内生成的证书总数。

我设法计算了时间增量,但我不知道如何收集计算值的最小值/最大值/平均值。 (我想我必须在elapsed 值上运行平均值)但我做不到。我必须声明elapsed 才能对其执行计算吗?

set serveroutput on
whenever sqlerror exit sql.sqlcode;
set echo off
SET VERIFY OFF
set heading off
SET FEEDBACK OFF
set serveroutput on size unlimited
set linesize 300
ALTER session SET TIME_ZONE='GMT';
Declare
vv_SchemaName VARCHAR2(15);
BEGIN
vv_SchemaName := Sys_Context('USERENV','CURRENT_SCHEMA');
FOR REVOKED IN (
select  a.transaction_id as trans_Id,
        e.eventcode,
        e.eventtime,
        a.time_received,
        round((e.eventtime - a.time_received) * 86400) as elapsed
  from      request_history_tbl a 
        join
            eventlog_tbl e
          on a.transaction_id = e.transaction_id
  where e.eventtime >= current_timestamp - 2
    and e.eventcode = '50'
    )
    LOOP
    dbms_output.put_line('average_request_time' || ...)
    dbms_output.put_line('Min_request_time' || ...)
    dbms_output.put_line('max_request_time' || ...)
   
    END LOOP;
END;

如何获得 dbms_output.put_lines 中的 AVG/MIN/MAX?

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    如果我对您的理解正确,那么您编写的查询可能会用作 CTE(或内联视图),即您将用来获得所需内容的 source:

    with temp as
      (select a.transaction_id,
              e.eventcode,
              e.eventtime,
              a.time_received,
              (e.eventtime - a.time_received) * 86400  as diff_in_seconds       
       from request_history_tbl a join eventlog_tbl e
         on a.transaction_id = e.transaction_id
       where e.eventtime >= current_timestamp - 10/1440
         and e.eventcode = '50'
      )
    select avg(diff_in_seconds) avg_diff,
           min(diff_in_seconds) min_diff,
           max(diff_in_seconds) max_diff
    from temp;
    

    切换到 PL/SQL 很容易;在游标 FOR 循环中使用该查询并显示结果:

    begin
      for cur_r in 
        (select avg(diff_in_seconds) avg_diff,
                min(diff_in_seconds) min_diff,
                max(diff_in_seconds) max_diff
         from (select a.transaction_id,
                 e.eventcode,
                 e.eventtime,
                 a.time_received,
                 (e.eventtime - a.time_received) * 86400  as diff_in_seconds       
               from request_history_tbl a join eventlog_tbl e
                 on a.transaction_id = e.transaction_id
               where e.eventtime >= current_timestamp - 10/1440
                 and e.eventcode = '50'
              )
        ) loop
          dbms_output.put_line(cur_r.avg_diff ||', '||
                               cur_r.min_diff ||', '||
                               cur_r.max_diff);
      end loop;
    end;
    

    【讨论】:

    • 我更新了原来的问题。你能帮我得到 dbms_output 行中的 avg/min/max 吗?
    猜你喜欢
    • 2016-11-04
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    • 2021-01-25
    • 2020-03-01
    相关资源
    最近更新 更多