为什么我的 case 语句没有返回 "else" 子句中的值?
Why is my case statement not returning the value in my "else" clause?
我有这部分代码,但我的 ELSE 不工作。它应该 return 'UNKNOWN' 但我仍然在我的结果中看到 NULL。怎么回事?
case
when t2.NEURO_GRP_2 is not NULL then t2.NEURO_GRP_1 --ASSIGN GROUP 2 BASED ON PRIMARY DX CODE
when t2.NEURO_GRP_2 is null AND t6.NEURO_GRP_2 IS NOT NULL then t6.NEURO_GRP_2 --ASSIGN GROUP 2 BASED ON CLINIC
when t2.NEURO_GRP_2 is null AND t6.NEURO_GRP_2 IS NULL then t3.NEURO_GRP_3 --ASSIGN GROUP 2 BASED ON PROCEDURE GROUP
ELSE 'UNKNOWN'
end AS NEURO_GRP_2,
看起来 coalesce()
确实如您所愿:
coalesce(t2.NEURO_GRP_2, t6.NEURO_GRP_2, t3.NEURO_GRP_3, 'UNKNOWN') S NEURO_GRP_2
您的原始代码的问题可能是:
第一个条件不匹配(您检查一列是否为空,但 return 另一列)
条件有点重复,没有正确级联
您的意思可能是:
case
when t2.NEURO_GRP_2 IS NOT NULL then t2.NEURO_GRP_2
when t6.NEURO_GRP_2 IS NOT NULL then t6.NEURO_GRP_2
when t3.NEURO_GRP_3 IS NOT NULL then t3.NEURO_GRP_3
else 'UNKNOWN'
end AS NEURO_GRP_2
...这正是 coalesce()
所做的。
也许你想要 coalesce()
:
coalesce(t2.NEURO_GRP_2, t6.NEURO_GRP_2, t3.NEURO_GRP_3, 'UNKNOWN')
这 return 是第一个非 NULL
值。正如最后一个参数用常数表示的那样,它永远不会 return NULL
.
我有这部分代码,但我的 ELSE 不工作。它应该 return 'UNKNOWN' 但我仍然在我的结果中看到 NULL。怎么回事?
case
when t2.NEURO_GRP_2 is not NULL then t2.NEURO_GRP_1 --ASSIGN GROUP 2 BASED ON PRIMARY DX CODE
when t2.NEURO_GRP_2 is null AND t6.NEURO_GRP_2 IS NOT NULL then t6.NEURO_GRP_2 --ASSIGN GROUP 2 BASED ON CLINIC
when t2.NEURO_GRP_2 is null AND t6.NEURO_GRP_2 IS NULL then t3.NEURO_GRP_3 --ASSIGN GROUP 2 BASED ON PROCEDURE GROUP
ELSE 'UNKNOWN'
end AS NEURO_GRP_2,
看起来 coalesce()
确实如您所愿:
coalesce(t2.NEURO_GRP_2, t6.NEURO_GRP_2, t3.NEURO_GRP_3, 'UNKNOWN') S NEURO_GRP_2
您的原始代码的问题可能是:
第一个条件不匹配(您检查一列是否为空,但 return 另一列)
条件有点重复,没有正确级联
您的意思可能是:
case
when t2.NEURO_GRP_2 IS NOT NULL then t2.NEURO_GRP_2
when t6.NEURO_GRP_2 IS NOT NULL then t6.NEURO_GRP_2
when t3.NEURO_GRP_3 IS NOT NULL then t3.NEURO_GRP_3
else 'UNKNOWN'
end AS NEURO_GRP_2
...这正是 coalesce()
所做的。
也许你想要 coalesce()
:
coalesce(t2.NEURO_GRP_2, t6.NEURO_GRP_2, t3.NEURO_GRP_3, 'UNKNOWN')
这 return 是第一个非 NULL
值。正如最后一个参数用常数表示的那样,它永远不会 return NULL
.