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() は 2 つのパラメーターを取ります。NullIf()のドキュメントを検索しましたか?

2 つの式が等しくない場合、NULLIF は最初の式を返します。式が等しい場合、NULLIF は最初の式の型の null 値を返します。

以下に例を示します: http://www.bennadel.com/blog/984-Using-NULLIF-To-Prevent-Divide-By-Zero-Errors-In-SQL.htm

于 2011-12-27T21:15:11.250 に答える