Select 为什么向我的查询添加条件会导致ORA-00934错误消息?

Select 为什么向我的查询添加条件会导致ORA-00934错误消息?,select,plsql,ora-00934,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.maxexten

我有以下疑问:

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。那么,如何在原始查询中包含上述条件呢?

使用having子句

你可以在这里查阅:你可以对你遇到的大多数错误都这样做,它会澄清错误的含义和可能的原因,因为它完全说明了这个错误。用户hotfix给出的答案是正确的。
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;