2011-12-27 5 views
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> 

誰も私の構文を明確にすることができますしてください?

答えて

3

NullIf()は2つのパラメータをとります。 NullIf()のマニュアルを検索しましたか?

NULLIFは、2つの式が でなければ最初の式を返します。式が等しい場合、NULLIFは最初の式の型であるヌル値 を返します。

ここでは一例です:http://www.bennadel.com/blog/984-Using-NULLIF-To-Prevent-Divide-By-Zero-Errors-In-SQL.htm

+0

+1彼らは 'NULLIF(expression_that_might_be_zero、0)'ゼロはNULL' 'に変換取得し、それがエラーを起こさないようにする必要があります。 –

+0

私はこれがここにあると信じています:(nullIf(docunitprice * orderqty)、0)。これは間違っていますか? – aparker81

+0

@ aparker81の場合、あなたはあなたの 'nullif'ステートメントの一つにその構文しかありません。 – Ben