我想在SELECT子句中执行除法。当我连接一些表并使用聚合函数时,我经常使用null或零值作为分隔符。至于现在我只想出这种避免除零和零值的方法。
选择 date_part('周',startmeasurement :: date)AS周,
(COUNT(CASE WHEN new_spm.status IN ('Closed','Resolved')THEN 1 ELSE NULL END)
*100/count(case when new_spm.status !='Cancelled' THEN 1 ELSE NULL END)::double precision) AS percentage_closed_and_resolved
from new_spm
WHERE new_spm.divisi='CNOS-HQ'
GROUP BY week;
答案 0 :(得分:0)
查看postgresql中提供的COALESCE表达式。它应该大大简化您当前的方法。
https://www.postgresql.org/docs/current/static/functions-conditional.html