@@header

/*
*
*  Author     : Vishal Gupta
*  Purpose    : Display all wait events for a SQL_ID from all sessions
*  Parameters : 1 - SQL_ID
*               
*
*  Revision History:
*  ===================
*  Date       Author        Description
*  ---------  ------------  -----------------------------------------
*  14-May-12  Vishal Gupta  Intial version
*
*/


DEFINE sqlid="&&1"

COLUMN INST_ID                HEADING "I#"              FORMAT 99
COLUMN SPID                                             FORMAT a6
COLUMN SID                                              FORMAT 9999
COLUMN serial#                HEADING "Serial#"         FORMAT 99999
COLUMN username                                         FORMAT a20
COLUMN last_call_et           HEADING "Last Call (s)"   FORMAT 99,999        
COLUMN logon_time                                       FORMAT a15
COLUMN service_name                                     FORMAT a21 
COLUMN process                                          FORMAT a15
COLUMN osuser                                           FORMAT a10 TRUNCATE
COLUMN wait_class      HEAD "WaitClass"          FORMAT a20 TRUNCATE
COLUMN event           HEAD "EventName"          FORMAT a40
COLUMN total_waits     HEAD "TotalWaits"
COLUMN total_timeouts  HEAD "TotalTimeOuts"
COLUMN time_waited     HEAD "TimeWaited (s)"
COLUMN average_wait    HEAD "AvgWait (s)"
COLUMN max_wait        HEAD "MaxWait (s)"

SELECT * FROM 
(
SELECT e.sid
     , e.inst_id
     , s.username
     , s.osuser
     , e.wait_class
     , e.event
     , e.total_waits
     , e.total_timeouts
     , e.time_waited / 100 time_waited
     , e.average_wait / 100 average_wait
     , e.max_wait    / 100 max_wait
     , s.program
  FROM gv$session s
     , gv$session_event e
     , gv$sqlarea sql
 WHERE s.inst_id = sql.inst_id
   AND s.sql_id = sql.sql_id
   AND s.inst_id = e.inst_id
   AND s.sid = e.sid
   AND e.wait_class <> 'Idle'
   AND s.sql_id = '&&sqlid'
ORDER BY e.time_waited_micro desc
)
where rownum <= 45
;

UNDEFINE sqlid


@@footer
