[英]Calculate average/min/max/total in calculated values
EDIT: ADDED PL/SQL script:编辑:添加 PL/SQL 脚本:
I am running a script that calculates the time difference between two columns which are on different tables.我正在运行一个脚本来计算不同表上的两列之间的时间差。 It basically calculates the time a certificate took to be generated by the CA.它主要计算 CA 生成证书所花费的时间。 I want to run average/min/max and the total of certificates generated in the last 10 minutes.我想运行平均/最小/最大和过去 10 分钟内生成的证书总数。
I managed to calculate the time delta but I do not get how can I gather the min/max/average on the calculated values.我设法计算了时间增量,但我不知道如何收集计算值的最小值/最大值/平均值。 (I think I have to run the average on the elapsed
value) but I do not manage. (我认为我必须计算elapsed
值的平均值)但我无法管理。 Do I have to declare elapsed
to be able to perform calculations on it?我是否必须声明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;
how can i get the AVG/MIN/MAX in the dbms_output.put_lines?如何在 dbms_output.put_lines 中获得 AVG/MIN/MAX?
If I understood you correctly, then query you wrote might be used as a CTE (or an inline view), ie a source you'll use to get what you want:如果我理解正确,那么您编写的查询可能会用作 CTE(或内联视图),即您将用来获得所需内容的源:
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;
Switching to PL/SQL is easy;切换到 PL/SQL 很容易; use that query in cursor FOR loop and display the result:在 cursor 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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.