with stats as ( select /*+ materialize */ * from ( select t.start#,t.end#,t.sql_id,t.plan_hash_value,t.execs,t.avg_sec,s.sql_text from ( select min(begin_interval_time) start#,max(begin_interval_time) end#, sql_id, plan_hash_value, sum(nvl(executions_delta,0)) execs, round ((sum(elapsed_time_delta)/(sum(executions_delta)))/1000000) avg_sec from DBA_HIST_SQLSTAT S, DBA_HIST_SNAPSHOT SS where 1=1 and ss.begin_interval_time > (sysdate -&2/1440) and ss.snap_id = S.snap_id and ss.instance_number = S.instance_number and executions_delta > 0 and (elapsed_time_delta/decode(nvl(executions_delta,0),0,1,executions_delta))/1000000 > &1 group by sql_id, plan_hash_value )t join dba_hist_sqltext s on s.sql_id=t.sql_id order by execs*avg_sec desc ) tt where rownum <= 50 ) select * from stats s where 1=1 and s.sql_text not like '%/* DS_SVC */%' and s.sql_text not like '%_job_proc%' and s.sql_text not like '%SQL Analyze%' and lower(s.sql_text) not like '%dbms_feature%' and s.sql_text not like '%SYS_DBA_SEGS%' and lower(s.sql_text) not like '%v$sys%' and lower(s.sql_text) not like '%dba_audit_trail%' and lower(s.sql_text) not like '%no_index(mytab)%';