2017-03-08 75 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