在我的查询中,在计算平均值时,我遇到了除以零的错误。我试图通过使用 Nullif 来解决这个问题,但我认为我的语法不正确,因为 Coldfusion 会抛出一个错误,指出')' 附近的语法不正确。
我的查询是:
<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>
任何人都可以为我澄清语法吗?