Sql 除以零时遇到错误

Sql 除以零时遇到错误,sql,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_g

有人能帮我从这个查询中找出这个错误吗。我能弄明白

谢谢

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

使用case构造检查零:

, 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 NAME:'是非标准的。是别名的标准。

p21\u销售\u历史记录\u报告\u视图。销售\u价格将为零。问题是什么?当p21\u sales\u history\u report\u view.sales\u price=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%'