我如何制作一个IIF,ELSEIF,以比较Null的值和帐户

问题描述

我正在尝试将Field1与Field2的值进行比较,并包括其中一个字段(或在某些情况下)都存在Null的情况。

Field1 | Field2 | Expected      | Condition
-----------------------------------------------------------------
A      | A      | Pass          | Perfect Match
A      | B      | Fail          | Value ofI Field1 does not match Field2
A      | Null   | Investigate   | Field1 has a value but Field2 does not
Null   | Null   | No Version    | Field1 is null and Field2 is null
Null   | A      | Error         | Field1 is null Field2 has a value

这是iif,否则我变幻了,但不断抛出错误

=iif((Field1 = Field2,"Pass","Fail") ELSEIF (Field1 NOT NULL,and Field2 IS NULL,"Manual","Error"))

从那里开始,事情变得疯狂起来。

我开始认为最好在sql中使用CASE,因为它更干净,但是我不确定方法

解决方法

我会用例。

Case when field1 is null and field2 is not null then 'Error'
     when field1 is null and field2 is null then 'No Version'
     when field1 is not null and field2 is null then 'Investigate'
     when field1=field2 then 'Pass'
     when field1<> field2 'Fail' 
     else 'Unhandled Case' end as Expected

从长远来看,更容易查看业务规则和维护。

或构建您调用的函数,该函数确定该值以便于重复使用。如果您希望一遍又一遍地使用这些业务规则。

,

case会在第一次成功时停止,因此,如果您订购测试,则可以简化逻辑。

case when Field1 is null and Field2 is null then 'No Version'
when Field1 is null then 'Error'
when Field2 is null then 'Investigate'
when Field1 <> Field2 then 'Fail'
else 'Pass'
end