1
在我的查詢中,當計算平均值時,我遇到一個被零除的錯誤。我試圖通過使用Nullif來解決這個問題,但我認爲我的語法不正確,因爲Coldfusion在'''附近拋出一個錯誤提示錯誤的語法。NullIf()防止被零除
我的查詢是:
<cfquery name="getValueAdd" datasource="#myDSN#">
select d.partnum, sum(docunitprice * orderqty) as total_sales,
sum((c.avglaborcost + c.avgburdencost + c.avgmaterialcost + c.avgsubcontcost + c.avgmtlburcost)*d.orderqty) as total_cost,
sum((docunitprice * orderqty)-((c.avglaborcost + c.avgburdencost + c.avgmaterialcost + c.avgsubcontcost + c.avgmtlburcost)*d.orderqty)) as Value_add,
avg (isNull(
((((docunitprice * orderqty)-((c.avglaborcost + c.avgburdencost + c.avgmaterialcost + c.avgsubcontcost + c.avgmtlburcost)*d.orderqty))/ (nullIf(docunitprice * orderqty), 0),0)
))) as PercValueAdd
from orderhed h with(nolock), orderdtl d with(nolock), partcost c with(nolock)
where h.company = 'PC68300'
and d.company = h.company
and c.company = h.company
and d.ordernum = h.ordernum
and c.partnum = d.partnum
and hdcasenum = <cfqueryparam cfsqltype="cf_sql_integer" value="#rc.hdcasenum#" />
group by d.partnum
</cfquery>
誰能澄清語法我好嗎?
+1他們需要'NULLIF(expression_that_might_be_zero,0)'使零地轉化爲'NULL',它並不會導致錯誤。 – 2011-12-27 21:28:32
我相信我有這裏:(nullIf(docunitprice * orderqty),0)。這是不正確的? – aparker81 2011-12-28 14:13:57
@ aparker81,你只在你的'nullif'語句中有一個語法,而不是兩個。 – Ben 2011-12-28 14:59:50