Rollup 函数和总百分比值

Rollup function and percentage value in total

我尝试使用 ROLLUP 函数显示表空间使用情况,结果看起来不错。但是,如您所见,总计的 percent_free 只是一个总和,而不是百分比。 你能帮我更改以下查询以获得总计的百分比值吗?

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,
    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


TABLESPACE      TOTAL_SPACE_MB USED_SPACE_MB FREE_SPACE_MB PERCENT_FREE
--------------- -------------- ------------- ------------- ------------
DEMO                      1500            47          1453        96.87
NORMAL                      10             1             9        88.75
SYSAUX                    7990          7600           390         4.88
SYSTEM                     970           961             9           .9
UNDOTBS1                  1690            24          1666        98.57
USERS                     5275           964          4311        81.73
Total                    17435          9597          7838        371.7

您可以为此使用 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_name 为空)时,对整个 table 的总和使用该轮次。然后在 DECODE 的 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