@@header

/*
*
*  Author        : Vishal Gupta
*  Purpose       : Display Session details from ASH
*  Compatibility : 10.1 and above
*  Parameters    : 1 - SID
*                  2 - INST_ID         (optional, default to 1)
*                  3 - Serial#         (Default %)
*                  4 - Number of hours (Default 24)
*                  
*
*  Revision History:
*  ===================
*  Date       Author        Description
*  ---------  ------------  -----------------------------------------
*  31-Dec-15  Vishal Gupta  Added duration in the output
*  03-FEB-14  Vishal Gupta  Created
*
*/


/************************************
*  INPUT PARAMETERS
************************************/
UNDEFINE SID
UNDEFINE INST_ID
UNDEFINE SERIAL
UNDEFINE HOURS

DEFINE SID="&&1"
DEFINE INST_ID="&&2"
DEFINE SERIAL="&&3"
DEFINE HOURS="&&4"
DEFINE WHERECLAUSE="&&5"

COLUMN  _SID          NEW_VALUE  SID         NOPRINT
COLUMN  _INST_ID      NEW_VALUE  INST_ID     NOPRINT
COLUMN  _SERIAL       NEW_VALUE  SERIAL      NOPRINT
COLUMN  _HOURS        NEW_VALUE  HOURS       NOPRINT

set term off
SELECT DECODE(UPPER('&&SID'),'','%',UPPER('&&SID'))         "_SID"
     , DECODE(UPPER('&&INST_ID'),'','1',UPPER('&&INST_ID')) "_INST_ID"
     , DECODE(UPPER('&&SERIAL'),'','%',UPPER('&&SERIAL'))   "_SERIAL"
     , DECODE(UPPER('&&HOURS'),'','24',UPPER('&&HOURS'))    "_HOURS"
FROM DUAL;
set term on
/***********************************/


PROMPT ***************************************************
PROMPT * ASH SQL Details
PROMPT *
PROMPT * Input Parameter:
PROMPT *   SID             = "&&SID"
PROMPT *   Instance ID     = "&&INST_ID"
PROMPT *   Serial#         = "&&SERIAL"
PROMPT *   Number of Hours = "&&HOURS"
PROMPT *   WhereClause     = "&&WHERECLAUSE"
PROMPT ***************************************************

COLUMN session_id                 HEADING "SID"              FORMAT 99999
COLUMN inst_id                    HEADING "I#"               FORMAT 99
COLUMN "session_serial#"          HEADING "Serial#"          FORMAT 999999
COLUMN sample_time_min                                       FORMAT a18
COLUMN sample_time_max                                       FORMAT a18
COLUMN duration                   HEADING "Duration|+D HH:MM:SS"      FORMAT a12
COLUMN username                   HEADING "DBUser"           FORMAT a18 
COLUMN service_name               HEADING "ServiceName"      FORMAT a21
COLUMN program                    HEADING "Program"          FORMAT a25 TRUNCATE
COLUMN machine                    HEADING "Machine"          FORMAT a25 TRUNCATE

-- Get the SQL Statements from ASH
SELECT /*+ parallel(ash, 10) */ 
       --ash.sql_exec_id,
       NVL(ash.qc_session_id,ash.session_id)                                        session_id 
     , NVL(ash.qc_instance_id,ash.inst_id)                                          inst_id     
     , NVL(ash.qc_session_serial#,ash.session_serial#)                              session_serial#
     , TO_CHAR(MIN(ash.sample_time),'DD-MON-YY HH24:MI:SS')                         sample_time_min
     , TO_CHAR(max(ash.sample_time) ,'DD-MON-YY HH24:MI:SS')                        sample_time_max
     , SUBSTR(REPLACE(max(ash.sample_time) - MIN(ash.sample_time),'+00000000','+')
              ,1,INSTR(REPLACE(max(ash.sample_time) - MIN(ash.sample_time),'+00000000','+'),'.')-1       
              ) duration
     , u.username
     , s.name                                                                       service_name
     , NVL(ash_parent.program,ash.program)                                          program
     , ash.machine
    FROM gv$active_session_history ash
         JOIN dba_users u ON u.user_id = ash.user_id
         JOIN dba_services s ON s.name_hash = ash.service_hash
         LEFT OUTER JOIN gv$active_session_history ash_parent 
                     ON ash_parent.inst_id                   = ash.qc_instance_id
                    AND ash_parent.session_id                = ash.qc_session_id
                    AND ash_parent.session_serial#           = ash.qc_session_serial#
                    AND CAST(ash_parent.sample_time as DATE) = ash.sql_exec_start
   WHERE NVL(ash.qc_instance_id,ash.inst_id)              LIKE '&&INST_ID'  
     AND NVL(ash.qc_session_id,ash.session_id)            LIKE '&&SID' 
     AND NVL(ash.qc_session_serial#,ash.session_serial#)  LIKE '&&SERIAL'
     AND ash.sample_time                                  > sysdate - (&&HOURS/24)
     AND (ash_parent.sample_time IS NULL or ash_parent.sample_time > sysdate - (&&HOURS/24))
     &&WHERECLAUSE
GROUP BY NVL(ash.qc_session_id,ash.session_id)
       , NVL(ash.qc_instance_id,ash.inst_id)   
       , NVL(ash.qc_session_serial#,ash.session_serial#)
       , u.username
       , s.name
       , NVL(ash_parent.program,ash.program)
       , ash.machine
ORDER BY MIN(ash.sample_time) ASC   
       , max(ash.sample_time) asc
;


@@footer
