我有一个量表,显示值标签和百分比。它基于返回VALUE和MAX_VALUE的查询。在某些情况下,查询返回0作为MAX_VALUE,然后图表显示消息“无效数据”。我相信这是由于被零除而发生的。如何防止显示此消息并返回0作为VALUE和MAX_VALUE以及0%?
SELECT NUM_FAILED VALUE, TOTAL_NUM MAX_VALUE
FROM
(
SELECT (
SELECT COUNT(*) FROM (select t1.name, t1.run_id
from Table1 t1
LEFT JOIN Table2 t2 ON t1.ID=t2.ID
WHERE t1.START_DATE > SYSDATE - 1
GROUP BY t1.name, t1.run_id)
) AS TOTAL_NUM,
(
SELECT COUNT(*) FROM (select name, run_id
from Table1 t1
LEFT JOIN Tabe2 t2 ON t1.ID=t2.ID
where t1.run_id IN (SELECT run_id FROM Table1 where err_code > 0)
AND t1.START_DATE > SYSDATE - 1
GROUP BY t1.name, t1.run_id)
) as NUM_FAILED
FROM dual)
答案 0 :(得分:1)
感谢您发布查询。
如果要避免出现0,DECODE
怎么样?
SELECT decode(NUM_FAILED, 0, 1E99, NUM_FAILED) VALUE,
decode(TOTAL_NUM , 0, 1E99, TOTAL_NUM ) MAX_VALUE
FROM (the rest of your query goes here)
您没有说明随后发生的情况(即是什么导致零除的确切原因-是NUM_FAILED
还是TOTAL_NUM
)?这个想法是:不是除以零,而是除以一个非常大的值(例如1E99),您会得到一个很小的数字,例如:
SQL> select 24/1E99 from dual;
24/1E99
----------
2,4000E-98
SQL>
,实际上是零。看看是否可以用这种方法做些什么。