set linesize 500
col tbs format A20;
col total_g format 999999999;
col used_g format 999999999;
col max_total_g format 999999999;
col remained_g format 999999999;
col used_rate format A10;
col remained_rate format A15;
col max_used_rate format A15;
col max_remained_rate format A18;
WITH w1 AS
(SELECT b.file_id,
b.tablespace_name tbs,
b.file_name NAME,
round(b.bytes / 1024 / 1024 / 1024, 2) AS total_g,
round(b.maxbytes / 1024 / 1024 / 1024, 2) AS max_total_g,
round((b.bytes -
decode(SUM(nvl(a.bytes, 0)), 0, 1, SUM(nvl(a.bytes, 0)))) / 1024 / 1024 / 1024,
2) AS used_g,
round(SUM(nvl(a.bytes, 0)) / 1024 / 1024 / 1024, 2) AS remained_g
FROM dba_free_space a, dba_data_files b
WHERE a.file_id = b.file_id
GROUP BY b.file_id, b.tablespace_name, b.file_name, b.bytes, b.maxbytes)
SELECT w1.tbs,
SUM(total_g) AS total_g,
SUM(max_total_g) AS max_total_g,
SUM(used_g) AS used_g,
SUM(remained_g) AS remained_g,
round(SUM(used_g) / decode(SUM(total_g), 0, 1, SUM(total_g)) * 100,
2) || '%' AS used_rate,
round(SUM(remained_g) / decode(SUM(total_g), 0, 1, SUM(total_g)) * 100,
2) || '%' AS remained_rate,
(CASE
WHEN SUM(max_total_g) <> 0 THEN
round(SUM(used_g) /
decode(SUM(max_total_g), 0, 1, SUM(max_total_g)) * 100,
2) || '%'
ELSE
'-'
END) AS max_used_rate,
(CASE
WHEN SUM(max_total_g) <> 0 THEN
round((SUM(max_total_g) - SUM(used_g)) /
decode(SUM(max_total_g), 0, 1, SUM(max_total_g)) * 100,
2) || '%'
ELSE
'-'
END) AS max_remained_rate
FROM w1
GROUP BY w1.tbs;