为什么在查询中添加条件会导致ORA-00934错误消息?

时间:2018-08-09 12:52:40

标签: select plsql ora-00934

我有以下查询:

select a.tablespace_name "Tab_name", 
ROUND(SUM(a.bytes)/1024/1024/1024, 2) "Sum_files_GB", 
ROUND(SUM(decode(b.maxextend, null, A.BYTES/1024/1024/1024, b.maxextend*C.BLOCK_SIZE/1024/1024/1024)), 2) "Max_size_GB",
ROUND((SUM(decode(b.maxextend, null, A.BYTES/1024/1024/1024, b.maxextend*C.BLOCK_SIZE/1024/1024/1024)) - (SUM(a.bytes)/1024/1024/1024 - round(c."Free"/1024/1024/1024))), 2) "Free_GB",
round(100*(SUM(a.bytes)/1024/1024/1024 - round(c."Free"/1024/1024/1024))/(SUM(decode(b.maxextend, null, A.BYTES/1024/1024/1024, b.maxextend*C.BLOCK_SIZE/1024/1024/1024)))) "%_used"
from dba_data_files a, sys.filext$ b, (SELECT d.tablespace_name , sum(nvl(c.bytes,0)) "Free",D.BLOCK_SIZE FROM dba_tablespaces d,DBA_FREE_SPACE c where d.tablespace_name = c.tablespace_name(+) group by d.tablespace_name, D.BLOCK_SIZE) c
where a.file_id = b.file#(+) 
and a.tablespace_name = c.tablespace_name 
GROUP by a.tablespace_name, c."Free"
order by round(100*(SUM(a.bytes)/1024/1024/1024 - round(c."Free"/1024/1024/1024))/(SUM(decode(b.maxextend, null, A.BYTES/1024/1024/1024, b.maxextend*C.BLOCK_SIZE/1024/1024/1024)))) desc;

我要添加以下条件:

and round(100*(SUM(a.bytes)/1024/1024/1024 - round(c."Free"/1024/1024/1024))/(SUM(decode(b.maxextend, null, A.BYTES/1024/1024/1024, b.maxextend*C.BLOCK_SIZE/1024/1024/1024)))) "%_used" < 30
and ROUND((SUM(decode(b.maxextend, null, A.BYTES/1024/1024/1024, b.maxextend*C.BLOCK_SIZE/1024/1024/1024)) - (SUM(a.bytes)/1024/1024/1024 - round(c."Free"/1024/1024/1024))), 2) "Free_GB" < 99

结果,查询如下:

select a.tablespace_name "Tab_name", 
ROUND(SUM(a.bytes)/1024/1024/1024, 2) "Sum_files_GB", 
ROUND(SUM(decode(b.maxextend, null, A.BYTES/1024/1024/1024, b.maxextend*C.BLOCK_SIZE/1024/1024/1024)), 2) "Max_size_GB",
ROUND((SUM(decode(b.maxextend, null, A.BYTES/1024/1024/1024, b.maxextend*C.BLOCK_SIZE/1024/1024/1024)) - (SUM(a.bytes)/1024/1024/1024 - round(c."Free"/1024/1024/1024))), 2) "Free_GB",
round(100*(SUM(a.bytes)/1024/1024/1024 - round(c."Free"/1024/1024/1024))/(SUM(decode(b.maxextend, null, A.BYTES/1024/1024/1024, b.maxextend*C.BLOCK_SIZE/1024/1024/1024)))) "%_used"
from dba_data_files a, sys.filext$ b, (SELECT d.tablespace_name , sum(nvl(c.bytes,0)) "Free",D.BLOCK_SIZE FROM dba_tablespaces d,DBA_FREE_SPACE c where d.tablespace_name = c.tablespace_name(+) group by d.tablespace_name, D.BLOCK_SIZE) c
where a.file_id = b.file#(+) 
and a.tablespace_name = c.tablespace_name 
and round(100*(SUM(a.bytes)/1024/1024/1024 - round(c."Free"/1024/1024/1024))/(SUM(decode(b.maxextend, null, A.BYTES/1024/1024/1024, b.maxextend*C.BLOCK_SIZE/1024/1024/1024)))) "%_used" < 30
and ROUND((SUM(decode(b.maxextend, null, A.BYTES/1024/1024/1024, b.maxextend*C.BLOCK_SIZE/1024/1024/1024)) - (SUM(a.bytes)/1024/1024/1024 - round(c."Free"/1024/1024/1024))), 2) "Free_GB" < 99
GROUP by a.tablespace_name, c."Free"
order by round(100*(SUM(a.bytes)/1024/1024/1024 - round(c."Free"/1024/1024/1024))/(SUM(decode(b.maxextend, null, A.BYTES/1024/1024/1024, b.maxextend*C.BLOCK_SIZE/1024/1024/1024)))) desc;

但是此查询给我错误消息ORA-00934。那么,如何在原始查询中包括上述条件?

1 个答案:

答案 0 :(得分:1)

使用having子句

select a.tablespace_name "Tab_name", 
       ROUND(SUM(a.bytes)/1024/1024/1024, 2) "Sum_files_GB", 
       ROUND(SUM(decode(b.maxextend, null, A.BYTES/1024/1024/1024, b.maxextend*C.BLOCK_SIZE/1024/1024/1024)), 2) "Max_size_GB",
       ROUND((SUM(decode(b.maxextend, null, A.BYTES/1024/1024/1024, b.maxextend*C.BLOCK_SIZE/1024/1024/1024)) - (SUM(a.bytes)/1024/1024/1024 - round(c."Free"/1024/1024/1024))), 2) "Free_GB",
       round(100*(SUM(a.bytes)/1024/1024/1024 - round(c."Free"/1024/1024/1024))/(SUM(decode(b.maxextend, null, A.BYTES/1024/1024/1024, b.maxextend*C.BLOCK_SIZE/1024/1024/1024)))) "%_used"
from dba_data_files a, sys.filext$ b, (SELECT d.tablespace_name , sum(nvl(c.bytes,0)) "Free",D.BLOCK_SIZE FROM dba_tablespaces d,DBA_FREE_SPACE c where d.tablespace_name = c.tablespace_name(+) group by d.tablespace_name, D.BLOCK_SIZE) c
where a.file_id = b.file#(+) 
  and a.tablespace_name = c.tablespace_name 
GROUP by a.tablespace_name, c."Free"
having round(100*(SUM(a.bytes)/1024/1024/1024 - round(c."Free"/1024/1024/1024))/(SUM(decode(b.maxextend, null, A.BYTES/1024/1024/1024, b.maxextend*C.BLOCK_SIZE/1024/1024/1024)))) < 30
   and ROUND((SUM(decode(b.maxextend, null, A.BYTES/1024/1024/1024, b.maxextend*C.BLOCK_SIZE/1024/1024/1024)) - (SUM(a.bytes)/1024/1024/1024 - round(c."Free"/1024/1024/1024))), 2)  < 99
order by round(100*(SUM(a.bytes)/1024/1024/1024 - round(c."Free"/1024/1024/1024))/(SUM(decode(b.maxextend, null, A.BYTES/1024/1024/1024, b.maxextend*C.BLOCK_SIZE/1024/1024/1024)))) desc;