来自Case When Result时的表达式的情况

时间:2018-06-08 20:03:17

标签: sql-server case

我需要在表达式从前一个案例的结果表达式时写出一个案例。

 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'

提前谢谢!

1 个答案:

答案 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'

IIF