除以零错误遇到

时间:2015-01-20 14:26:01

标签: sql

有人可以帮助我从此查询中获取此错误。我可以搞清楚。

由于

SELECT p21_sales_history_report_view.invoice_date AS 'Invoice Date'
, p21_sales_history_report_view.customer_id AS 'Cust. Id'
, p21_sales_history_report_view.customer_name AS 'Cust. Name'
, inv_mast.default_purchase_disc_group AS 'Division'
, p21_sales_history_report_view.product_group_desc AS 'Product Group'
, inv_mast.class_id1 AS 'Brand'
, p21_sales_history_report_view.invoice_no AS 'Inv. No.'
, p21_sales_history_report_view.item_id AS 'Item ID'
, p21_sales_history_report_view.item_desc AS 'Item Description'
, p21_sales_history_report_view.qty_shipped AS 'Qty'
, p21_sales_history_report_view.sales_price AS 'Net Sales'
, p21_sales_history_report_view.cogs_amount AS 'Total Cost'
, p21_sales_history_report_view.total_sales AS 'Invoice Amount'
, p21_sales_history_report_view.other_charge_amount AS 'Discount'
, p21_sales_history_report_view.sales_location_id
, (p21_sales_history_report_view.sales_price-p21_sales_history_report_view.cogs_amount)/p21_sales_history_report_view.sales_price AS 'GM%'  
FROM 
    P21.dbo.inv_mast inv_mast, 
    P21.dbo.p21_sales_history_report_view p21_sales_history_report_view  
WHERE inv_mast.item_id = p21_sales_history_report_view.item_id
    AND ((p21_sales_history_report_view.customer_id=?) AND (p21_sales_history_report_view.year_for_period=?) 
    AND (p21_sales_history_report_view.other_charge_item='N') 
    AND (inv_mast.default_purchase_disc_group<>'PARTS'))  
ORDER BY p21_sales_history_report_view.product_group_desc

结果

Customer ID Year
3   3
Invoice Date    Cust. Id    Cust. Name  Division    Product Group   Brand   Inv. No.    Item ID Item Description

1 个答案:

答案 0 :(得分:0)

使用案例构造检查零:

, case when p21_sales_history_report_view.sales_price = 0 then 
    null 
  else 
    (p21_sales_history_report_view.sales_price-p21_sales_history_report_view.cogs_amount)/p21_sales_history_report_view.sales_price 
  end AS 'GM%'  

顺便说一句:如果你使用较短的别名来表示你的查询会更好阅读。列aslias名称:'是非标准的。 “是别名的标准。