set echo off
TTITLE  "Space Deficit Report"
COLUMN owner FORMAT a10 HEADING Owner ON
COLUMN segment_name FORMAT a15 HEADING "Segment Name" ON
COLUMN segment_type FORMAT a10 HEADING "Segment Type" ON
COLUMN tablespace_name FORMAT a10 HEADING "Tablespace" ON
COLUMN initial_extent HEADING "Initial|Extent(Mb)" JUSTIFY left ON  FORMAT 999,999
COLUMN alloc HEADING "Alloc|(Mb)" JUSTIFY left ON  FORMAT 999,999
COLUMN next_extent HEADING "Next|Extent(Mb)" JUSTIFY left ON  FORMAT 999,999
COLUMN free_extent HEADING "Free|Extent(Mb)" JUSTIFY left ON FORMAT 999,999
COLUMN total_free HEADING "Total|Free(Mb)" JUSTIFY left ON FORMAT 999,999
COLUMN pct_increase HEADING "%Inc" FORMAT 999
set pages 32676 lines 200 



select a.owner
     , decode(partition_name, null, segment_name, segment_name || ':' || partition_name) segment_name
     , a.SEGMENT_TYPE
     , a.tablespace_name
     , a.bytes/1024/1024 alloc
     , a.initial_extent/1024/1024 initial_extent
     , a.next_extent/1024/1024  next_extent
     , a.PCT_INCREASE
     , a.extents
     , a.max_extents
     , b.free_extent/1024/1024 free_extent
     , b.total_free/1024/1024 total_free
     , decode(c.autoextensible, 0, 'NO', 'YES') autoextensible 
     , decode(c.autoextensible, 0, 0, c.morebytes) max_can_grow_by 
     , decode(c.autoextensible, 0, 0, c.totalmorebytes) sum_can_grow_by
from   dba_segments a,
       (select   df.tablespace_name
               , nvl(max(fs.bytes), 0) free_extent
               , nvl(sum(fs.bytes), 0) total_free
        from     dba_data_files df,
                 dba_free_space fs
        where    df.file_id = fs.file_id (+)
        group by df.tablespace_name) b
        ,(select  tablespace_name, max(maxbytes - bytes) morebytes,
                  sum(decode(AUTOEXTENSIBLE, 'YES', maxbytes - bytes, 0)) totalmorebytes,
                  sum(decode(AUTOEXTENSIBLE, 'YES', 1, 0)) autoextensible
         from     dba_data_files
         group by tablespace_name) c
where a.tablespace_name = b.tablespace_name
and   a.tablespace_name = c.tablespace_name
and   ((c.autoextensible = 0) or ((c.autoextensible > 0) 
and   (a.next_extent > c.morebytes)))
and   (a.next_extent > b.free_extent OR a.extents >= a.max_extents)
order by 5 desc, 3;


TTITLE OFF
COLUMN owner OFF
COLUMN segment_name OFF
COLUMN segment_type OFF
COLUMN tablespace_name OFF
COLUMN next_extent OFF
COLUMN free_extent OFF
set pages 14 lines 80 numf ""
