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