我需要在表达式从前一个案例的结果表达式时写出一个案例。
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'
提前谢谢!
答案 0 :(得分:0)
嵌套的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'