我有一个类似的数据集;
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硬编码为Y,将NO硬编码为N。
我通过在外部查询中添加一条case语句得到了结果。有没有像嵌套case这样的替代方法。
我还可以使用列别名newcol来应用case条件吗
发布于 2017-03-08 19:08:17
您可以在case周围添加case表达式,如下所示:
case (
case
when col1 = 1 then col2
when col1 = 2 then col3
end
) when 'YES' then 'Y' else 'N' end as newcolSUBSTR会选择不带条件的YES或NO的第一个字符:
SUBSTR(
case
when col1 = 1 then col2
when col1 = 2 then col3
end
, 1
, 1
) as newcolhttps://stackoverflow.com/questions/42669530
复制相似问题