@@header

/*
*
*  Author        : Vishal Gupta
*  Purpose       : Display Session's SQL IDs
*  Compatibility : 10.1 and above
*  Parameters    : 1 - SID
*                  2 - INST_ID (optional, default to 1)
*
*  Revision History:
*  ===================
*  Date       Author        Description
*  ---------  ------------  -----------------------------------------
*  05-SEP-14  Vishal Gupta  Removed filter for SQL_EXEC_START IS NOT NULL
*  28-Jun-12  Vishal Gupta  Created
*
*/

VARIABLE SID number ;
VARIABLE INST_ID number ;

BEGIN
    :SID := &&1;
    :INST_ID := NVL(&&2,1);
    IF :INST_ID = '' OR :INST_ID IS NULL THEN
       :INST_ID := 1;
    END IF;   
END;
/
 
PROMPT 
PROMPT ######### ASH SQL Ids ##########################

COLUMN session_id                 HEADING "SID"                       FORMAT 99999
COLUMN instance_number            HEADING "I#"                        FORMAT 99
COLUMN inst_id                    HEADING "I#"                        FORMAT 99
COLUMN "session_serial#"          HEADING "Serial#"                   FORMAT 999999
COLUMN FORCE_MATCHING_SIGNATURE   HEADING "Force|Matching|Signature"  FORMAT 99999999999999999999999
COLUMN sql_plan_hash_value        HEADING "Plan|Hash|Value"           FORMAT 9999999999 
COLUMN sql_exec_start                                                 FORMAT a18
COLUMN sql_exec_end               HEADING "MaxSampleTime"             FORMAT a18
COLUMN duration                   HEADING "Duration|+D HH:MM:SS"      FORMAT a12
COLUMN sql_opname                 HEADING "SQL|Operation"             FORMAT a10 TRUNCATE
COLUMN sql_child_number           HEADING "SQL|Ch#"                   FORMAT 999
COLUMN current_dop                HEADING "DOP"                       FORMAT 999
COLUMN phyread                    HEADING "Phy|Read|(GB)"             FORMAT 9999
COLUMN phywrite                   HEADING "Phy|Write|(GB)"            FORMAT 9999
COLUMN interconnect_io            HEADING "Inter|Connect|IO|(GB)"     FORMAT 9999
COLUMN pga_allocated              HEADING "PGA|(GB)"                  FORMAT 99.00
COLUMN temp_space_allocated       HEADING "Temp|Space|(GB)"           FORMAT 999.00

-- Get the SQL Statements from ASH
SELECT --ash.sql_exec_id,
       --TO_CHAR(NVL(ash.sql_exec_start,MIN(ash.sample_time)),'DD-MON-YY HH24:MI:SS') sql_exec_start
          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(NVL(ash.sql_exec_start,MIN(ash.sample_time)),'DD-MON-YY HH24:MI:SS') sql_exec_start
     , TO_CHAR(max(ash.sample_time) ,'DD-MON-YY HH24:MI:SS')                    sql_exec_end
     , SUBSTR(REPLACE(max(ash.sample_time) - NVL(CAST(ash.sql_exec_start as TIMESTAMP(3)),MIN(ash.sample_time)),'+00000000','+')
              ,1,INSTR(REPLACE(max(ash.sample_time) - NVL(CAST(ash.sql_exec_start as TIMESTAMP(3)),MIN(ash.sample_time)),'+00000000','+'),'.')-1       
              ) duration
&&_IF_ORA_11gR2_OR_HIGHER , ash.sql_opname
     , ash.sql_id
     , ash.sql_child_number
     , ash.sql_plan_hash_value
&&_IF_ORA_11202_OR_HIGHER , max(trunc(ash.px_flags / 2097152))    current_dop
     , ash.force_matching_signature                               force_matching_signature
&&_IF_ORA_11gR1_OR_HIGHER , NVL(ash_parent.top_level_sql_id,ash.top_level_sql_id)      top_level_sql_id
     , ROUND(SUM(ash.delta_read_io_bytes)/power(1024,3))          phyread
     , ROUND(SUM(ash.delta_write_io_bytes)/power(1024,3))         phywrite
     , ROUND(SUM(ash.delta_interconnect_io_bytes)/power(1024,3))  interconnect_io
     , ROUND(MAX(ash.pga_allocated)/power(1024,3),2)              pga_allocated
     , ROUND(MAX(ash.temp_space_allocated)/power(1024,3),2)       temp_space_allocated
  FROM gv$session s
       JOIN gv$active_session_history ash 
                    ON s.inst_id = NVL(ash.qc_instance_id,ash.inst_id)
                   AND s.sid     = NVL(ash.qc_session_id,ash.session_id)
                   AND s.serial# = NVL(ash.qc_session_serial#,ash.session_serial#)
       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 s.inst_id    = :INST_ID
   AND s.sid        = :SID
   --AND ash.sql_exec_id IS NOT NULL
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#)
       , ash.sql_exec_id
       , ash.sql_exec_start
       , ash.sql_id
       , ash.sql_child_number
       , ash.sql_plan_hash_value
       , ash.FORCE_MATCHING_SIGNATURE
&&_IF_ORA_11gR2_OR_HIGHER , ash.sql_opname
&&_IF_ORA_11gR1_OR_HIGHER , NVL(ash_parent.top_level_sql_id,ash.top_level_sql_id)
ORDER BY 
        -- max(ash.sample_time) asc
       --, 
       max(ash.sample_time) asc
&&_IF_ORA_11gR1_OR_HIGHER     , NVL(ash.sql_exec_start,MIN(ash.sample_time)) ASC   
;


BEGIN
    :SID := NULL;
    :INST_ID := NULL;
END;
/


@@footer
