Sum列查询结果在单个字段中,而不是单独的列

时间:2015-07-28 08:16:35

标签: sql sum

我有一个查询,它显示高水位线以上的Oracle数据库数据文件可用空间,我希望在结尾处有一个字段,表示" Total"列的总和" FreeSize(Mb)"。我只能在一个单独的列中执行此操作:

sum( (bytes/1024/1024) - ceil( (nvl(hwm,1)* (size_db_block))/1024/1024 ) ) OVER() "Total"

如何在一个字段中显示?感谢。

SELECT 'alter database datafile '''||file_name||''' resize '|| 
    DECODE(trunc(ceil( (nvl(hwm,1)*(size_db_block))/1024/1024 ) /10),0 ,10, ceil((nvl(hwm,1)* (size_db_block))/1024/1024 )) ||'M;' "Resize Command", 
    AUTOEXTENSIBLE , bytes/1024/1024 "CurrentSize(Mb)" , 
    ((bytes/1024/1024) - ceil((nvl(hwm,1)* (size_db_block))/1024/1024)) "FreeSize(Mb)", sum((bytes/1024/1024) - ceil((nvl(hwm,1)* (size_db_block))/1024/1024)) OVER() "Total"
FROM dba_data_files a, 
    (SELECT file_id, max(block_id+blocks-1) AS hwm FROM dba_extents GROUP BY file_id) b,
    (SELECT TO_NUMBER(value) AS size_db_block FROM v$parameter WHERE name = 'db_block_size') c
WHERE a.file_id = b.file_id(+) AND AUTOEXTENSIBLE='YES' 
AND ceil(blocks*(c.size_db_block)/1024/1024)- ceil((nvl(hwm,1)*(c.size_db_block))/1024/1024 ) > 10
ORDER BY "FreeSize(Mb)";

当前输出:

Resize Command CurrentSize(Mb) FreeSize(Mb)      Total
------------------------------ ------------ ----------
datafile 1                 830           38        617
datafile 2                 100           65        617
datafile 3                 100           80        617
datafile 4                 100           80        617
datafile 5                 390          354        617

但我希望它显示:

Resize Command CurrentSize(Mb) FreeSize(Mb)
------------------------------ ------------
datafile 1                 830           38
datafile 2                 100           65
datafile 3                 100           80
datafile 4                 100           80
datafile 5                 390          354
                               Total:   617

1 个答案:

答案 0 :(得分:1)

您在RDBMS术语中无法添加作为输出的内容。您需要一个报告工具才能以该格式显示。如果你纯粹想要在工具(SQL +或toad)中显示,你可以做的是在输出中添加一个额外的行,它将保存自由大小(总自由大小)的总和值。

SELECT 'alter database datafile '''||file_name||''' resize '|| 
    DECODE(trunc(ceil( (nvl(hwm,1)*(size_db_block))/1024/1024 ) /10),0 ,10, ceil((nvl(hwm,1)* (size_db_block))/1024/1024 )) ||'M;' "Resize Command", 
    AUTOEXTENSIBLE , bytes/1024/1024 "CurrentSize(Mb)" , 
    ((bytes/1024/1024) - ceil((nvl(hwm,1)* (size_db_block))/1024/1024)) "FreeSize(Mb)"

FROM dba_data_files a, 
    (SELECT file_id, max(block_id+blocks-1) AS hwm FROM dba_extents GROUP BY file_id) b,
    (SELECT TO_NUMBER(value) AS size_db_block FROM v$parameter WHERE name = 'db_block_size') c
WHERE a.file_id = b.file_id(+) AND AUTOEXTENSIBLE='YES' 
AND ceil(blocks*(c.size_db_block)/1024/1024)- ceil((nvl(hwm,1)*(c.size_db_block))/1024/1024 ) > 10
ORDER BY "FreeSize(Mb)"

UNION 


SELECT 'Total Free Size', AUTOEXTENSIBLE ,
    '', 
    sum((bytes/1024/1024) - ceil((nvl(hwm,1)* (size_db_block))/1024/1024)) OVER() "Total"
FROM dba_data_files a, 
    (SELECT file_id, max(block_id+blocks-1) AS hwm FROM dba_extents GROUP BY file_id) b,
    (SELECT TO_NUMBER(value) AS size_db_block FROM v$parameter WHERE name = 'db_block_size') c
WHERE a.file_id = b.file_id(+) AND AUTOEXTENSIBLE='YES' 
AND ceil(blocks*(c.size_db_block)/1024/1024)- ceil((nvl(hwm,1)*(c.size_db_block))/1024/1024 ) > 10
ORDER BY "FreeSize(Mb)";