2017-03-08 77 views
2

我有一个类似这样的数据集;嵌套案例硬编码案例陈述的结果

col1 col2 col3 
1  YES NO 
2  NO YES 

我想申请case声明,

case when col1 = 1 then col2 
    when col1 = 2 then col3 end as newcol 

现在newcol将值作为输出YES/NO。是否可以在上面的case条件内应用另一个case,以便我可以将YES硬编码为YNO作为N

我通过在外部查询中添加case语句得到了结果。是否有任何替代方法,如嵌套case

也可以应用case条件使用列别名newcol

+0

我不跟着你。你可以添加示例数据显示你的意思吗? –

+0

@TimBiegeleisen在得到'case'语句的结果之后,我想添加另一个'case'来使'YES'成为'Y'而'NO'成为'N' –

回答

3

您可以添加的情况下,表达你的case周围,像这样:

case (
    case 
     when col1 = 1 then col2 
     when col1 = 2 then col3 
    end 
) when 'YES' then 'Y' else 'N' end as newcol 

SUBSTR会挑YESNO的第一个字符没有一个条件:

SUBSTR(
    case 
     when col1 = 1 then col2 
     when col1 = 2 then col3 
    end 
, 1 
, 1 
) as newcol