如何在此查询周围包装 DECODE 以处理零除数而不计算分母总和两次?thedate
除了四舍五入的百分比之外,我不想返回任何内容。
SELECT thedate, ROUND (100*
SUM( case when TRUNC(ACTIVITY_END_DATE) <= thedate
AND TRUNC(ACTIVITY_END_DATE) >= add_months( trunc(thedate,'mm'), -12)
AND trunc(ACTIVITY_END_DATE) <= trunc(ACTIVITY_NEED_DATE)
AND SYSDATE >= trunc(thedate,'mm') then 1 else 0 end )
/
SUM( case when TRUNC(ACTIVITY_END_DATE) <= thedate
AND TRUNC(ACTIVITY_END_DATE) >= add_months( trunc(thedate,'mm'), -12)
AND SYSDATE >= trunc(thedate,'mm') then 1 else 0 end ) ) as OTR12
FROM TEST
cross join ( select add_months(last_day(SYSDATE), level-7) as thedate
from dual connect by level <= 12 )
GROUP BY thedate
ORDER BY thedate
似乎使用解码,我必须做两次分母求和
DECODE(denominator_summation,0,NULL, numerator_summation / denominator_summation)