Sql Case语句硬编码结果的嵌套Case
我有一个类似的数据集Sql Case语句硬编码结果的嵌套Case,sql,oracle,Sql,Oracle,我有一个类似的数据集 col1 col2 col3 1 YES NO 2 NO YES 我正在尝试应用案例语句 case when col1 = 1 then col2 when col1 = 2 then col3 end as newcol 现在,newcol的输出值将为YES/NO。是否可以在上述案例条件内应用另一个案例,以便我可以将YES硬编码为Y和NO为N 我通过在外部查询中添加case语句得到了结果。是否有其他方法,如嵌套案例 还可以使用列别名ne
col1 col2 col3
1 YES NO
2 NO YES
我正在尝试应用案例
语句
case when col1 = 1 then col2
when col1 = 2 then col3 end as newcol
现在,newcol
的输出值将为YES/NO
。是否可以在上述案例
条件内应用另一个案例
,以便我可以将YES
硬编码为Y
和NO
为N
我通过在外部查询中添加case
语句得到了结果。是否有其他方法,如嵌套案例
还可以使用列别名
newcol
应用case
条件吗?您可以在case
周围添加一个case表达式,如下所示:
case (
case
when col1 = 1 then col2
when col1 = 2 then col3
end
) when 'YES' then 'Y' else 'N' end as newcol
SUBSTR
将选择YES
或NO
的第一个字符,但不带条件:
SUBSTR(
case
when col1 = 1 then col2
when col1 = 2 then col3
end
, 1
, 1
) as newcol
我不明白你的意思。你能添加示例数据来说明你的意思吗?@TimBiegeleisen在获取了
case
语句的结果后,我想添加另一个case
以使YES
成为Y
和NO
成为N