您可以使用
DECODE
为此:
DECODE(df.tablespace_name, NULL,
ROUND(100*SUM(fs.FREE_SPACE)/SUM(df.total_space),2),
SUM(ROUND(100 * (fs.free_space / df.total_space),2))) PERCENT_FREE,
当它是“Total”行(tablespace\u name为null)时,对整个表的和进行四舍五入。然后像往常一样在解码的else子句中计算其他行。
SELECT nvl(df.tablespace_name,'Total') TABLESPACE,
sum(df.total_space_mb) TOTAL_SPACE_MB,
sum((df.total_space_mb - fs.free_space_mb)) USED_SPACE_MB,
sum(fs.free_space_mb) FREE_SPACE_MB,
DECODE(df.tablespace_name, NULL,
ROUND(100*SUM(fs.FREE_SPACE)/SUM(df.total_space),2),
SUM(ROUND(100 * (fs.free_space / df.total_space),2))) PERCENT_FREE
FROM (SELECT tablespace_name, SUM(bytes) TOTAL_SPACE,
ROUND(SUM(bytes) / 1048576) TOTAL_SPACE_MB
FROM dba_data_files
GROUP BY tablespace_name) df,
(SELECT tablespace_name, SUM(bytes) FREE_SPACE,
ROUND(SUM(bytes) / 1048576) FREE_SPACE_MB
FROM dba_free_space
GROUP BY tablespace_name) fs
WHERE df.tablespace_name = fs.tablespace_name(+)
GROUP BY rollup(df.tablespace_name)
ORDER BY df.tablespace_name