1

在我的查询中,在计算平均值时,我遇到了除以零的错误。我试图通过使用 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>

任何人都可以为我澄清语法吗?

4

1 回答 1

3

NullIf() 接受两个参数。您是否搜索过NullIf()文档?

如果两个表达式不相等,则 NULLIF 返回第一个表达式。如果表达式相等,则 NULLIF 返回第一个表达式类型的空值。

这是一个例子:http ://www.bennadel.com/blog/984-Using-NULLIF-To-Prevent-Divide-By-Zero-Errors-In-SQL.htm

于 2011-12-27T21:15:11.250 回答