使用 oracle decode 解码两个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18646317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
decode two values using oracle decode
提问by user2750658
select status_a,status_b from test
how to decode status_a
,status_b
below values using oracle decode function aand if one of the values of status_a or status_b is null.
如何解码status_a
,status_b
下面使用Oracle解码功能A和如果status_a或status_b的值中的一个是空值。
if status_a='Y' and status_b='Y' then 'Y'
if status_a='Y' and status_b='N' then 'N'
if status_a='N' and status_b='Y' then 'N'
if status_a='N' and status_b='N' then 'N'
Regards,
问候,
Chaituhara
柴图哈拉
回答by Justin Cave
Why do you want to use DECODE
? CASE
would seem like a much better fit
你为什么要使用DECODE
? CASE
看起来更合适
CASE WHEN status_a = 'Y' and status_b = 'Y' THEN 'Y'
WHEN status_a = 'Y' and status_b = 'N' THEN 'N'
WHEN status_a = 'N' and status_b = 'Y' THEN 'N'
WHEN status_a = 'N' and status_b = 'N' THEN 'N'
END
Of course, the logic you posted does not appear to make sense. The only way that status_a = 'Y' or status_b = 'Y'
would evaluate to FALSE while status_a = 'Y' or status_b = 'N'
evaluated to TRUE would be if status_a = 'N'
and status_b = 'N'
. But that means that the third and fourth branch would never be reached. If you meant and
rather than or
, the logic would make sense. But in that case, you could simplify it to
当然,您发布的逻辑似乎没有意义。status_a = 'Y' or status_b = 'Y'
在status_a = 'Y' or status_b = 'N'
评估为 TRUE 时评估为 FALSE的唯一方法是 ifstatus_a = 'N'
和status_b = 'N'
。但这意味着永远不会到达第三和第四分支。如果您的意思是and
而不是or
,那么逻辑就有意义了。但在这种情况下,您可以将其简化为
CASE WHEN status_a = 'Y' and status_b = 'Y' THEN 'Y'
ELSE 'N'
END
回答by ChrisProsser
If you mean AND rather than OR then this can be done using a decode:
如果你的意思是 AND 而不是 OR 那么这可以使用解码来完成:
decode(status_a,'Y',
decode(status_b,'Y','Y','N'),
'N')
or this could be simplified to:
或者这可以简化为:
decode(status_a||status_b,'YY','Y','N')
回答by kishore krv
Based on your last comment I guessing ,you are saying that either of value is Y then the result should be Y and also in case either one is null then also it should be Y.
根据我猜您的最后一条评论,您是说其中一个值为 Y,则结果应为 Y,如果任一值为空,则结果也应为 Y。
I hope below decode works fine to meet your requirement.
我希望下面的解码可以很好地满足您的要求。
decode(status_a||Status_b,'YY','Y','YN','Y,'NY','Y','NN','N','Y')