2015-01-20 2 views
-4

Может кто-то помочь мне получить эту ошибку из этого запроса. Я могу понять это., деленная на нулевую погрешность

Благодаря

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

' p21_sales_history_report_view.sales_price' подходит как ноль. В чем вопрос? – DLeh

+0

http://stackoverflow.com/questions/861778/how-to-avoid-the-divide-by -zero-error-in-sql? rq = 1 –

+1

Каков ожидаемый результат, когда p21_sales_history_report_view.sales_price = 0? – jarlh

ответ

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%' 

BTW: Ваш запрос будет гораздо лучше читаемым, если вы использовали короче псевдонимы для таблиц. Что касается имен столбцов aslias: «нестандартно. "является стандартом для псевдонимов.

Смежные вопросы