select (select decode(extent_management,'LOCAL','*',' ')
from dba_tablespaces where tablespace_name = b.tablespace_name) || nvl(b.tablespace_name,
nvl(a.tablespace_name,'UNKOWN')) name,
(decode( kbytes_max, 0, 0, (kbytes_alloc/kbytes_max)*100)) pct_max_used
from ( select sum(bytes)/1024 Kbytes_free,
max(bytes)/1024 largest,
tablespace_name
from sys.dba_free_space
group by tablespace_name, bytes) a,
( select sum(bytes)/1024 Kbytes_alloc,
sum(maxbytes)/1024 Kbytes_max,
tablespace_name
from sys.dba_data_files
group by tablespace_name
union all
select sum(bytes)/1024 Kbytes_alloc,
sum(maxbytes)/1024 Kbytes_max,
tablespace_name
from sys.dba_temp_files
group by tablespace_name )b
where a.tablespace_name (+) = b.tablespace_name
Move dba_tablespaces to the from clause and group by the fields that are not aggregated. b.tablespace_name is never null, so nvl is superfluous:
select decode(c.extent_management,'LOCAL','*',' ') || b.tablespace_name name,
max(trunc((decode( kbytes_max, 0, 0, (kbytes_alloc/kbytes_max)*100)))) pct_max_used
from (select sum(bytes)/1024 Kbytes_free,
max(bytes)/1024 largest,
tablespace_name
from sys.dba_free_space
group by tablespace_name, bytes) a,
(select sum(bytes)/1024 Kbytes_alloc,
sum(maxbytes)/1024 Kbytes_max,
tablespace_name
from sys.dba_data_files
group by tablespace_name
union all
select sum(bytes)/1024 Kbytes_alloc,
sum(maxbytes)/1024 Kbytes_max,
tablespace_name
from sys.dba_temp_files
group by tablespace_name )b,
dba_tablespaces c
where a.tablespace_name (+) = b.tablespace_name
and c.tablespace_name (+) = b.tablespace_name
group by decode(c.extent_management,'LOCAL','*',' ') || b.tablespace_name
What does didn't work mean? You get wrong results or a syntax error? It works for me:
select * from
(select decode(c.extent_management,'LOCAL','*',' ') || b.tablespace_name name,
max(trunc((decode( kbytes_max, 0, 0, (kbytes_alloc/kbytes_max)*100)))) pct_max_used
from (select sum(bytes)/1024 Kbytes_free,
max(bytes)/1024 largest,
tablespace_name
from sys.dba_free_space
group by tablespace_name, bytes) a,
(select sum(bytes)/1024 Kbytes_alloc,
sum(maxbytes)/1024 Kbytes_max,
tablespace_name
from sys.dba_data_files
group by tablespace_name
union all
select sum(bytes)/1024 Kbytes_alloc,
sum(maxbytes)/1024 Kbytes_max,
tablespace_name
from sys.dba_temp_files
group by tablespace_name )b,
dba_tablespaces c
where a.tablespace_name (+) = b.tablespace_name
and c.tablespace_name (+) = b.tablespace_name
group by decode(c.extent_management,'LOCAL','*',' ') || b.tablespace_name
order by pct_max_used desc)
where rownum = 1
You have to use another layer of subquery because order by is applied last to the result set.