/*

Usage: 
    @sqlmon_lsrep <condition> <start_time> <end_time> <order_by_clause>

Examples:

    @sqlmon_lsrep 1=1 sysdate-1 sysdate
    @sqlmon_lsrep "sql_id='c5wnbwzzc7h3k'" sysdate-31 sysdate
    @sqlmon_lsrep 1=1 "timestamp'2023-02-12 19:31:10'" "timestamp'2023-02-12 22:00:31'"
    @sqlmon_lsrep "t.session_id=303 and session_serial#=59545"  sysdate-10 sysdate "6 asc"
    
*/ 

@@myheader.sql

COLUMN  _order_by_clause   NEW_VALUE order_by_clause      NOPRINT

set term off
SELECT
    DECODE('&&4','','1,4 ASC','&&4') "_order_by_clause"       
FROM DUAL;
set term on


PROMPT
PROMPT ***********************************************************************
PROMPT *  List SQL Monitor reports
PROMPT *
PROMPT *  Input Parameters 
PROMPT *  - where clause            = &&1
PROMPT *  - min timestamp           = &&2
PROMPT *  - min timestamp           = &&3
PROMPT *  - order by list           = &&order_by_clause
PROMPT ***********************************************************************

set pages 50

column report_id                   format  99999             heading 'Report|ID'
column status                      format  a15               heading 'Status'              
column sql_id                      format  a17               heading 'SQL_ID'
column plan_hash                   format  a15               heading 'Plan|hash value'
column sql_exec_start              format  a21               heading 'SQL|start'
column sql_exec_id                 format  a12               heading 'SQL|exec ID'
column elap_sec                    format  9999.99           heading 'Elapsed|sec'
column cpu_sec                     format  9999.99           heading 'CPU|sec'
column io_sec                      format  9999.99           heading 'IO|sec'
column buffer_gets                 format  a9                heading 'Buffer|gets'
column px_servers_requested        format  a3                heading 'PX|req'
column px_servers_allocated        format  a3                heading 'PW|all'
column sess                        format  a16               heading 'Session'
column username                    format  a15               heading 'Username'



SELECT /*+ NO_XML_QUERY_REWRITE */ 
      t.report_id
    , t.key1 sql_id
    , t.key2 sql_exec_id
    , to_char(to_date(t.key3,'MM:DD:YYYY HH24:MI:SS'),'YYYY-MM-DD HH24:MI:SS') sql_exec_start
    , x.plan_hash 
    , x.elapsed_time/1000000 elap_sec
    , x.cpu_time/1000000 cpu_sec
    , x.user_io_wait_time/1000000 io_sec
    , x.buffer_gets
    , x.username
    , ''''||t.session_id||','||t.session_serial#||'@'||t.instance_number||'''' sess
    , x.px_servers_requested
    , x.px_servers_allocated
    , x.status
FROM dba_hist_reports t    
   , xmltable('/report_repository_summary/sql'    
       PASSING xmlparse(document t.report_summary)    
       COLUMNS    
         sql_id                path '@sql_id'     
       , sql_exec_start        path '@sql_exec_start'    
       , sql_exec_id           path '@sql_exec_id'      
       , status                path 'status'    
       , sql_text              path 'sql_text'
       , first_refresh_time    path 'first_refresh_time'
       , last_refresh_time     path 'last_refresh_time'
       , refresh_count         path 'refresh_count'
       , inst_id               path 'inst_id'
       , session_id            path 'session_id'
       , session_serial        path 'session_serial'
       , user_id               path 'user_id'
       , username              path 'user'
       , con_id                path 'con_id'
       , con_name              path 'con_name'
       , modul                 path 'module'
       , action                path 'action'
       , service               path 'service'
       , program               path 'program'
       , plan_hash             path 'plan_hash'
       , is_cross_instance     path 'is_cross_instance'
       , dop                   path 'dop'
       , instances             path 'instances'
       , px_servers_requested  path 'px_servers_requested'
       , px_servers_allocated  path 'px_servers_allocated'
       , duration              path 'stats/stat[@name="duration"]'  
       , elapsed_time          path 'stats/stat[@name="elapsed_time"]'  
       , cpu_time              path 'stats/stat[@name="cpu_time"]'  
       , user_io_wait_time     path 'stats/stat[@name="user_io_wait_time"]'
       , application_wait_time path 'stats/stat[@name="application_wait_time"]'
       , concurrency_wait_time path 'stats/stat[@name="concurrency_wait_time"]'
       , cluster_wait_time     path 'stats/stat[@name="cluster_wait_time"]'
       , plsql_exec_time       path 'stats/stat[@name="plsql_exec_time"]'
       , other_wait_time       path 'stats/stat[@name="other_wait_time"]'
       , buffer_gets           path 'stats/stat[@name="buffer_gets"]'
       , read_reqs             path 'stats/stat[@name="read_reqs"]'
       , read_bytes            path 'stats/stat[@name="read_bytes"]'
     ) x 
where &&1
and   t.COMPONENT_NAME = 'sqlmonitor'
and to_date(t.key3,'MM:DD:YYYY HH24:MI:SS') between &&2 and &&3
order by &&order_by_clause
/			

@@myfooter.sql


