2017-04-21 85 views
1

为什么这给了我一个零误差的鸿沟?我希望这个返回的数据库中的记录数与声明的参数。谢谢您的帮助。为什么这给了我一个由零误差划分

SELECT COUNT(*) 
FROM dbcrms.ccars.ci_submission_history 
    where crms_dt = '2016-12-31' 
     and totalliabilities <> null or totalliabilities <> 0 
      and mra_required = 'Yes' 
       and committedexposure/totalliabilities <= 1 

我使用SQL Server 2014

+0

因为您忘记了括号 –

+0

总值为零 – Bestter

+0

因为至少有一行的总可用性值为0并且没有括号。 –

回答

6

你不能保证在您的条款将被评估的顺序,所以committedexposure/totalliabilities可之前totalliabilities <> 0

一种解决方法与nullif()设置0null评价:

SELECT COUNT(*) 
FROM dbcrms.ccars.ci_submission_history 
    where crms_dt = '2016-12-31' 
     and totalliabilities <> 0 
      and mra_required = 'Yes' 
       and committedexposure/nullif(totalliabilities,0) <= 1 

and <> null应该可能是is not null,如果是totalliabilities <> 0,那将是真的,所以它是多余的。

另外,请注意and takes precedence over or就逻辑运算符而言。最好是包装你的or的每一面,以便清楚你想要完成什么。

+0

太棒了。谢谢! –

+0

@DanielBailey乐意帮忙! – SqlZim

相关问题