我需要从前一个case when表达式的结果中编写一个case when表达式。
case
when state = 'nj' and date >= '2018-01-01' then 'new'
else 'existing'
end as 'new or existing'
case
when state = 'nj' and 'new or existing' = 'existing' and date <= '2017-12-31'
then 'eligible'
else 'next time'
end as 'eligibility'
提前谢谢你!
发布于 2018-06-09 04:09:47
嵌套的case when
可以做到这一点。
(case
when state='nj' and
(
case
when date < '2018-01-01' then 'existing'
end
)='existing' and date <= '2017-12-31' then 'eligible'
else 'next time'
end
) as 'eligibility'
理清你的逻辑,我想你需要这个。
(case
when state='nj' and date <= '2017-12-31' then 'eligible'
else 'next time'
end
) as 'eligibility'
您也可以使用IIF
来创建它。
IIF(state = 'nj' AND IIF(date < '2018-01-01','existing','new') ='existing' and date <= '2017-12-31' , 'eligible','next time') as 'eligibility'
https://stackoverflow.com/questions/50767693
复制相似问题