Example Oracle Sessions (Except Internal processes)

Description:
Example to List all Oracle Sessions (Except the Internal processes),
Check the example below:

------------------------------------------------------------------------------------------------------

set lines 2000
set heading on
set feedback off

prompt
prompt -- Sessions (Except INTERNAL Oracle)
prompt
col username    format A16 truncate    heading 'Username'
col oracle_id   format A10 truncate    heading 'Oracle id'
col osuser      format A40 truncate    heading 'osuser'
col pga         format A5  truncate    heading 'Pga'
col uga         format A5  truncate    heading 'Uga'
col pid         format 9999999         heading 'Pid' 
col Typ         format A3  truncate    heading 'Typ' 
col status      format A6  truncate    heading 'status'
col command     format A8  truncate    heading 'command'
col logon_time  format A18 truncate    heading 'Logon time'
col cpu_used    format 9999999         heading 'CPU used'
col Program     format A35 truncate    heading 'Program' 
col Module      format A35 truncate    heading 'Module' 

select decode(s.type,'BACKGROUND','INTERNAL'
              ,decode(u.password,'EXTERNAL',s.username||'/', s.username)
              ) username
      , substr( decode (s.machine,null ,'',s.machine||':')||decode(s.osuser,'OraUser'
              ,decode(dp.paddr,null
                      ,decode (ss.paddr,null
                               ,decode(sign(instr(upper(p.program),'TCP')),1,''
                                       ,decode(sign(instr(upper(p.program),'TNS')),1,'',''
                                              )
                                      )
                               ,''
                              )
                      ,''
                     )
                                                                   ,s.osuser
                                                                  )
              , 1, 50 
             ) OSuser
      , substr(p.spid,1,6) pid
      , substr(''''
             || s.sid
             || ','
             ||s.serial#
             ||''''
             || decode (s.status
                       ,'KILLED','*'
                                ,''
                       )
             ,1,10) oracle_id
      , substr(s.program,1,30) program
      , substr( decode( s.lockwait
                     , null
                     , decode( s.command,0,' ',
                                         1,'CRE TAB', 
                                         2,'INSERT', 
                                         3,'SELECT', 
                                         4,'CRE CLUSTER', 
                                         5,'ALT CLUSTER', 
                                         6,'UPDATE', 
                                         7,'DELETE', 
                                         8,'DRP CLUSTER', 
                                         9,'CRE INDEX', 
                                         10,'DROP INDEX', 
                                         11,'ALT INDEX', 
                                         12,'DROP TABLE', 
                                         13,'CRE SEQ', 
                                         14,'ALT SEQ', 
                                         15,'ALT TABLE', 
                                         16,'DROP SEQ', 
                                         17,'GRANT', 
                                         18,'REVOKE', 
                                         19,'CRE SYN', 
                                         20,'DROP SYN', 
                                         21,'CRE VIEW', 
                                         22,'DROP VIEW', 
                                         23,'VAL INDEX', 
                                         24,'CRE PROC', 
                                         25,'ALT PROC', 
                                         26,'LOCK TABLE', 
                                         28,'RENAME', 
                                         29,'COMMENT', 
                                         30,'AUDIT', 
                                         31,'NOAUDIT', 
                                         32,'CRE DBLINK', 
                                         33,'DROP DBLINK', 
                                         34,'CRE DB', 
                                         35,'ALTER DB', 
                                         36,'CRE RBS', 
                                         37,'ALT RBS', 
                                         38,'DROP RBS', 
                                         39,'CRE TBLSPC', 
                                         40,'ALT TBLSPC', 
                                         41,'DROP TBLSPC', 
                                         42,'ALT SESSION', 
                                         43,'ALT USER', 
                                         44,'COMMIT', 
                                         45,'ROLLBACK', 
                                         46,'SAVEPOINT', 
                                         47,'PL/SQL EXEC', 
                                         48,'SET XACTN', 
                                         49,'SWITCH LOG', 
                                         50,'EXPLAIN', 
                                         51,'CRE USER', 
                                         52,'CRE ROLE', 
                                         53,'DROP USER', 
                                         54,'DROP ROLE', 
                                         55,'SET ROLE', 
                                         56,'CRE SCHEMA', 
                                         57,'CRE CTLFILE', 
                                         58,'ALTER TRACING', 
                                         59,'CRE TRIGGER', 
                                         60,'ALT TRIGGER', 
                                         61,'DRP TRIGGER', 
                                         62,'ANALYZE TAB', 
                                         63,'ANALYZE IX', 
                                         64,'ANALYZE CLUS', 
                                         65,'CRE PROFILE', 
                                         66,'DRP PROFILE', 
                                         67,'ALT PROFILE', 
                                         68,'DRP PROC', 
                                         69,'DRP PROC', 
                                         70,'ALT RESOURCE', 
                                         71,'CRE SNPLOG', 
                                         72,'ALT SNPLOG', 
                                         73,'DROP SNPLOG', 
                                         74,'CREATE SNAP', 
                                         75,'ALT SNAP', 
                                         76,'DROP SNAP', 
                                         79,'ALTER ROLE', 
                                         79,'ALTER ROLE', 
                                         85,'TRUNC TAB', 
                                         86,'TRUNC CLUST', 
                                         88,'ALT VIEW', 
                                         91,'CRE FUNC', 
                                         92,'ALT FUNC', 
                                         93,'DROP FUNC', 
                                         94,'CRE PKG', 
                                         95,'ALT PKG', 
                                         96,'DROP PKG', 
                                         97,'CRE PKG BODY', 
                                         98,'ALT PKG BODY', 
                                         99,'DRP PKG BODY'
                             ,'OTH:'||to_char(s.command)
                             )
                     ,'waiting'
                     )
            ,1,7
            ) command
      , lower( decode ( s.status , 'INACTIVE' , '------'
                     , s.status
                     )
             ) status
      , s1.value cpu_used
      , substr(ltrim(to_char(round(pga.value/1024/1024,1),'9990D0')),1,5) as pga
      , substr(ltrim(to_char(round(uga.value/1024/1024,1),'9990D0')),1,5) as uga
      , to_char(s.logon_time, 'dd-mm-yyyy hh24:mi')  logon_time
from  sys.v_$sesstat       s1
,     sys.v_$process       p
,     sys.v_$sesstat       pga
,     sys.v_$sesstat       uga
,     sys.v_$session       s
,     sys.v_$dispatcher    dp
,     sys.v_$shared_server ss
,     sys.user$            u
where s1.statistic#        = 12
and   s1.sid               = s.sid
and   p.addr               = s.paddr
and   s.username           = u.name   (+)
and   p.addr               = ss.paddr (+)
and   p.addr               = dp.paddr (+)
and   uga.statistic#       = 15
and   pga.statistic#       = 20
and   s.sid                = uga.sid
and   s.sid                = pga.sid
and   nvl(s.osuser,'x')   <> 'SYSTEM'
and   s.type              <> 'BACKGROUND'
and   s.username is not null
order by s1.value
;

prompt
prompt -- Sessions TOAD, SQL*PLUS, OWB Client, SQL Developer
prompt
select decode(s.type
              ,'BACKGROUND','INTERNAL'
              ,decode(u.password
                     ,'EXTERNAL',s.username||'/'
                     , s.username
                     )
              ) username
      , substr( decode (s.machine
                      ,null ,''
                      ,s.machine||':')
                      || decode( s.osuser
                               ,'OraUser', decode(dp.paddr
                                                 ,null, decode (ss.paddr
                                                               ,null,decode(sign(instr(upper(p.program)
                                                               ,'TCP'
                                                               )
                                                 )
                               ,1,''
                               ,decode(sign(instr(upper(p.program),'TNS'))
                                           ,1,''
                                           ,''
                                      )
                               )
                      ,''
                      )
             ,''
             )
             ,s.osuser
                     )
             , 1, 40 ) OSuser
      , substr(p.spid,1,6) pid
      , substr(''''
             || s.sid
             || ','
             ||s.serial#
             ||''''
             || decode (s.status
                       ,'KILLED','*'
                                ,''
                       )
             ,1,10) oracle_id
      , substr(module,1,30) module
      , substr( decode( s.lockwait
                     , null
                     , decode( s.command,0,' ',
                                         1,'CRE TAB', 
                                         2,'INSERT', 
                                         3,'SELECT', 
                                         4,'CRE CLUSTER', 
                                         5,'ALT CLUSTER', 
                                         6,'UPDATE', 
                                         7,'DELETE', 
                                         8,'DRP CLUSTER', 
                                         9,'CRE INDEX', 
                                         10,'DROP INDEX', 
                                         11,'ALT INDEX', 
                                         12,'DROP TABLE', 
                                         13,'CRE SEQ', 
                                         14,'ALT SEQ', 
                                         15,'ALT TABLE', 
                                         16,'DROP SEQ', 
                                         17,'GRANT', 
                                         18,'REVOKE', 
                                         19,'CRE SYN', 
                                         20,'DROP SYN', 
                                         21,'CRE VIEW', 
                                         22,'DROP VIEW', 
                                         23,'VAL INDEX', 
                                         24,'CRE PROC', 
                                         25,'ALT PROC', 
                                         26,'LOCK TABLE', 
                                         28,'RENAME', 
                                         29,'COMMENT', 
                                         30,'AUDIT', 
                                         31,'NOAUDIT', 
                                         32,'CRE DBLINK', 
                                         33,'DROP DBLINK', 
                                         34,'CRE DB', 
                                         35,'ALTER DB', 
                                         36,'CRE RBS', 
                                         37,'ALT RBS', 
                                         38,'DROP RBS', 
                                         39,'CRE TBLSPC', 
                                         40,'ALT TBLSPC', 
                                         41,'DROP TBLSPC', 
                                         42,'ALT SESSION', 
                                         43,'ALT USER', 
                                         44,'COMMIT', 
                                         45,'ROLLBACK', 
                                         46,'SAVEPOINT', 
                                         47,'PL/SQL EXEC', 
                                         48,'SET XACTN', 
                                         49,'SWITCH LOG', 
                                         50,'EXPLAIN', 
                                         51,'CRE USER', 
                                         52,'CRE ROLE', 
                                         53,'DROP USER', 
                                         54,'DROP ROLE', 
                                         55,'SET ROLE', 
                                         56,'CRE SCHEMA', 
                                         57,'CRE CTLFILE', 
                                         58,'ALTER TRACING', 
                                         59,'CRE TRIGGER', 
                                         60,'ALT TRIGGER', 
                                         61,'DRP TRIGGER', 
                                         62,'ANALYZE TAB', 
                                         63,'ANALYZE IX', 
                                         64,'ANALYZE CLUS', 
                                         65,'CRE PROFILE', 
                                         66,'DRP PROFILE', 
                                         67,'ALT PROFILE', 
                                         68,'DRP PROC', 
                                         69,'DRP PROC', 
                                         70,'ALT RESOURCE', 
                                         71,'CRE SNPLOG', 
                                         72,'ALT SNPLOG', 
                                         73,'DROP SNPLOG', 
                                         74,'CREATE SNAP', 
                                         75,'ALT SNAP', 
                                         76,'DROP SNAP', 
                                         79,'ALTER ROLE', 
                                         79,'ALTER ROLE', 
                                         85,'TRUNC TAB', 
                                         86,'TRUNC CLUST', 
                                         88,'ALT VIEW', 
                                         91,'CRE FUNC', 
                                         92,'ALT FUNC', 
                                         93,'DROP FUNC', 
                                         94,'CRE PKG', 
                                         95,'ALT PKG', 
                                         96,'DROP PKG', 
                                         97,'CRE PKG BODY', 
                                         98,'ALT PKG BODY', 
                                         99,'DRP PKG BODY'
                             ,'OTH:'||to_char(s.command)
                             )
                     ,'waiting'
                     )
            ,1,7
            ) command
      , lower( decode ( s.status , 'INACTIVE' , '------'
                     , s.status
                     )
             ) status
      , s1.value cpu_used
      , substr(ltrim(to_char(round(pga.value/1024/1024,1),'9990D0')),1,5) pga
      , substr(ltrim(to_char(round(uga.value/1024/1024,1),'9990D0')),1,5) uga
      , to_char(s.logon_time, 'dd-mm-yyyy hh24:mi')  logon_time
from  sys.v_$sesstat       s1
,     sys.v_$process       p
,     sys.v_$sesstat       pga
,     sys.v_$sesstat       uga
,     sys.v_$session       s
,     sys.v_$dispatcher    dp
,     sys.v_$shared_server ss
,     sys.user$            u
where s1.statistic#        = 12
and   s1.sid               = s.sid
and   p.addr               = s.paddr
and   s.username           = u.name   (+)
and   p.addr               = ss.paddr (+)
and   p.addr               = dp.paddr (+)
and   uga.statistic#       = 15
and   pga.statistic#       = 20
and   s.sid                = uga.sid
and   s.sid                = pga.sid
and   nvl(s.osuser,'x')   <> 'SYSTEM'
and   s.type              <> 'BACKGROUND'
and   s.username is        not null
and   (upper(s.module)     like 'TOAD%'
       or upper(s.module)  like '%PLUS%'
--       or upper(s.program)  like '%PLUS%'
       or upper(s.module)  like '%PB%'
       or upper(s.module)  like '%WINNT%'
       or upper(s.module)  like '%ORACLE SQL DEVELOPER%'
      )
order by s1.value
;

prompt
prompt
set feedback on

break on report
compute sum of Session_Count on report

Prompt
Prompt Sessions by Program
Prompt
select   upper(nvl(program,'Unidentified')) "Program"
,        count(*) "Session_Count"
,        upper(nvl(module,'-')) "Module"
from     v$session
where    type     <> 'BACKGROUND'
and      username is not null
group by nvl(program,'Unidentified'), nvl(module,'-')
order by 1;

Prompt
Prompt Session by Username
Prompt
select   nvl(username,'Unidentified') "Username"
,        count(*) "Session_Count"
from     v$session
where      type     <> 'BACKGROUND'
group by nvl(username,'Unidentified')
order by 1;