[英]ERROR: division by zero + PostgreSQL + Rails
I'm getting the following error when I run this database view in psql (ERROR: division by zero).在 psql 中运行此数据库视图时出现以下错误(错误:除以零)。 Even I can't access my table in rails console If anyone has any solution for this error so please comment on it will be very helpful thanks in advance.即使我无法在 rails 控制台中访问我的表如果有人对此错误有任何解决方案,请对此发表评论,这将非常有帮助,提前致谢。
select
(exam_id || '00' || quarter)::bigint as id,
account_id,
exam_id,
quarter,
quarter_label,
count(id) as assessments,
max(banding_percentage) as top_assessment_percentage,
min(banding_percentage) as bottom_assessment_percentage,
round(avg(banding_percentage),2) as avg_assessment_percentage
from report_assessments
group by account_id, exam_id, quarter, quarter_label;
To avoid running into division by zero
exceptions you should use nullif
, so that you can catch this 0
and replace it by null
, eg为避免遇到division by zero
异常的情况,您应该使用nullif
,以便您可以捕获此0
并将其替换为null
,例如
WITH j (v1,v2) AS (
VALUES (1.0,0.0),(10.0,3.0)
)
SELECT v1/nullif(v2,0) AS div FROM j;
div
--------------------
3.3333333333333333
(2 Zeilen)
You Need to do something like this.你需要做这样的事情。 It works in my case:它适用于我的情况:
Negotiation.find(
:all,
conditions: conditions_hash('negotiation'),
select:
'CASE
sum(banding_percentage)
WHEN
0
THEN
NULL
ELSE
sum(total_percentage) / sum(banding_percentage)
END as error_check'
).first
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.