[英]NullIf() to Prevent Divide by Zero
在我的查询中,当计算平均值时,我遇到除以零的错误。 我试图通过使用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>
任何人都可以为我澄清语法吗?
NullIf()有两个参数。 您是否搜索过NullIf()文档?
如果两个表达式不相等,则NULLIF返回第一个表达式。 如果表达式相等,则NULLIF返回第一个表达式类型的空值。
以下是一个示例: http : //www.bennadel.com/blog/984-Using-NULLIF-To-Prevent-Divide-By-Zero-Errors-In-SQL.htm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.