重新生成这串sql 解决division by zero的问题 select PROD_DIS_TYPE , case when PROD_DIS_TYPE = '1' then DECODE(sum(FIRST_BIZ), 0,0, ROUND(sum(case when CONTRACT_CODE = 'NHT-1904506' then FIRST_BIZ else 0 end) / sum(FIRST_BIZ), 2))::int else 0 end as FIRST_BIZ , case when PROD_DIS_TYPE = '2' then DECODE(sum(APPR_DESIGN_PROD), 0, 0, ROUND(sum(case when CONTRACT_CODE = 'NHT-1904506' then APPR_DESIGN_PROD else 0 end) / sum(APPR_DESIGN_PROD), 2))::int else 0 end as APPR_DESIGN_PROD from T_SPDI_CONT_SUB_PROJ where UNITERM_CODE = 'N1900620-04-008' and SUB_PROJ_CODE = 'N1900620-04-302' and PROD_DIS_TYPE = '1' group by PROD_DIS_TYPE
时间: 2024-02-21 08:01:19 浏览: 75
可以在除法运算前加上 NULLIF 函数,将分母为 0 的情况转换为 NULL,避免了 division by zero 的错误。修改后的 SQL 如下:
```
SELECT
PROD_DIS_TYPE,
CASE WHEN PROD_DIS_TYPE = '1'
THEN DECODE(SUM(FIRST_BIZ), 0, 0, ROUND(SUM(CASE WHEN CONTRACT_CODE = 'NHT-1904506' THEN FIRST_BIZ ELSE 0 END) / NULLIF(SUM(FIRST_BIZ), 0), 2))::INT
ELSE 0
END AS FIRST_BIZ,
CASE WHEN PROD_DIS_TYPE = '2'
THEN DECODE(SUM(APPR_DESIGN_PROD), 0, 0, ROUND(SUM(CASE WHEN CONTRACT_CODE = 'NHT-1904506' THEN APPR_DESIGN_PROD ELSE 0 END) / NULLIF(SUM(APPR_DESIGN_PROD), 0), 2))::INT
ELSE 0
END AS APPR_DESIGN_PROD
FROM
T_SPDI_CONT_SUB_PROJ
WHERE
UNITERM_CODE = 'N1900620-04-008'
AND SUB_PROJ_CODE = 'N1900620-04-302'
AND PROD_DIS_TYPE = '1'
GROUP BY
PROD_DIS_TYPE
```
阅读全文