Informix除以零错误

时间:2016-03-07 23:50:12

标签: sql informix

我有一个错误输出错误的查询。我已经隔离了查询中哪些错误,并尝试使用NVL修复它,但它只是不顺利,有人能发现我的错误吗?

select  warehouse.ware_code,
    warehouse.desc_text,
    maingrp.maingrp_code,
    maingrp.desc_text,
    prodgrp.prodgrp_code,
    prodgrp.desc_text,
    left(product.part_code,7) Parent_Product_code,
    product.part_code,
    product.desc_text,
    sum(salesstat.order_qty - salesstat.return_qty) Total_Sales,
    sum((salesstat.order_qty - salesstat.return_qty) / 6) Average_Weekly_Sales,
    sum(((salesstat.order_qty - salesstat.return_qty) / 6) * 4) Average_Monthly_Sales,
    '??' Note,
    sum(prodstatus.onhand_qty) Stock_On_Hand,
    sum(orderstat.order_qty - orderstat.cancel_qty) Outstanding_orders,
    sum(prodstatus.onhand_qty - (orderstat.order_qty - orderstat.cancel_qty)) Stock_Available,
    --CASE
    --WHEN nvl(sum(salesstat.order_qty - salesstat.return_qty),0) = 0
    --  THEN 999
    --ELSE  sum(prodstatus.onhand_qty / ((salesstat.order_qty - salesstat.return_qty) / 6))
    --END as Weeks_Stock_Remaining,
    '??' Stock_ordered,
    '??' Production_date
from
    warehouse,
    warereptdetl,
    maingrp,
    prodgrp,
    product,
    prodstatus,
    salesstat,
    orderstat,
    Statint
where   warehouse.cmpy_code = maingrp.cmpy_code
and warehouse.cmpy_code = warereptdetl.cmpy_code
and warehouse.cmpy_code = prodgrp.cmpy_code
and warehouse.cmpy_code = product.cmpy_code
and warehouse.cmpy_code = salesstat.cmpy_code
and warehouse.cmpy_code = prodstatus.cmpy_code
and warehouse.cmpy_code = orderstat.cmpy_code
and warehouse.cmpy_code = statint.cmpy_code
and product.ware_code = prodstatus.ware_code
and product.ware_code = warehouse.ware_code
and product.ware_code = salesstat.ware_code
and product.ware_code = orderstat.ware_code
and product.ware_code = warereptdetl.ware_code
and product.part_code = salesstat.part_code
and product.part_code = orderstat.part_code
and product.part_code = prodstatus.part_code
and product.prodgrp_code = salesstat.prodgrp_code
and product.prodgrp_code = orderstat.prodgrp_code
and product.prodgrp_code = prodgrp.prodgrp_code
and product.maingrp_code = salesstat.maingrp_code
and product.maingrp_code = orderstat.maingrp_code
and product.maingrp_code = maingrp.maingrp_code
and salesstat.year_num = statint.year_num
and salesstat.int_num = statint.int_num
and salesstat.stat_type_code = statint.type_code
and orderstat.year_num = statint.year_num
and orderstat.int_num = statint.int_num
and orderstat.stat_type_code = statint.type_code
and warehouse.cmpy_code = 'AB'
and warereptdetl.warereptgrp_code = 'NSW'
and product.ware_code = 'HP1'
and product.status_ind <> 3
and left(product.part_code,2) = '21'
and statint.year_num = 2016
and statint.int_num between 29 AND 34
--and   statint.int_num = 29
and statint.type_code = 'WLY'
Group by 1,2,3,4,5,6,7,8,9

注释掉的部分是我试图修复的地方,如果我将它评论出来,那么查询运行正常(虽然有点慢)。

问题的第二部分是在没有访问我的数据库的情况下你可能无法回答的问题,但如果我运行查询1,2或甚至3周(statint.int_num),则查询会返回到1分钟。只要我推出6周(这就是我需要的),查询只需要40多分钟就可以返回结果。

我无法在查询中看到我创建笛卡尔的任何地方但是我已经盯着它看了很久我可能会错过它。

1 个答案:

答案 0 :(得分:2)

在您的案例陈述中:

sum(salesstat.order_qty - salesstat.return_qty)

可能不为零,但个人

salesstat.order_qty - salesstat.return_qty

这是你要除的,可能包含零。

修改

澄清,

sum(prodstatus.onhand_qty / ((salesstat.order_qty - salesstat.return_qty) / 6))

在denomiator中可能有零,因为它是几个分数的总和。仅仅因为所有分母总和为非零数并不意味着所有分母都不为零。