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