我希望将if语句复制到我的Hive代码中。本质上,我从2014年至2018年为相应的客户花了钱,我试图了解客户去年停止与我们一起花钱的情况,以及$的损失是基于前一年的花销。我无法执行CASE语句,因为每个IF语句都在寻找几个变量,但是我愿意接受建议!
If (FY_2014 > 0, (If(FY_2015 = 0, FY_2014,0)), 0) as '2014to2015'
If (FY_2015 > 0, (If(FY_2016 = 0, FY_2015,0)), 0) as '2015to2016'
If (FY_2016 > 0, (If(FY_2017 = 0, FY_2016,0)), 0) as '2016to2017'
If (FY_2017 > 0, (If(FY_2018 = 0, FY_2017,0)), 0) as'2017to2018'
在此先感谢您的帮助!
答案 0 :(得分:1)
我认为在较少的条件下这将完全相同。
case when FY_2014 > 0 AND FY_2015 = 0 then FY_2014 else 0 end as "2014to2015",
case when FY_2015 > 0 AND FY_2016 = 0 then FY_2015 else 0 end as "2015to2016",
case when FY_2016 > 0 AND FY_2017 = 0 then FY_2016 else 0 end as "2016to2017",
case when FY_2017 > 0 AND FY_2018 = 0 then FY_2017 else 0 end as "2017to2018"
答案 1 :(得分:0)
嵌套案例怎么样?
case when FY_2014 > 0 then case when FY_2015 = 0 then FY_2014 else 0 end else 0 end as "2014to2015",
case when FY_2015 > 0 then case when FY_2016 = 0 then FY_2015 else 0 end else 0 end as "2015to2016",
case when FY_2016 > 0 then case when FY_2017 = 0 then FY_2016 else 0 end else 0 end as "2016to2017",
case when FY_2017 > 0 then case when FY_2018 = 0 then FY_2017 else 0 end else 0 end as "2017to2018"