Skip to content
Advertisement

Hive query conditional statement in same select query

Is there a way to get in one single hive query to do a if-else kind of setup.

In the my data below I want to ensure that if Model is empty or having ‘-‘ I populate the Final column with Device else it should be populated with Model

My data is something like this 

Device  Model 
iPhone  6SPlus  
Samsung -     

Output I want is 
Device  Model   Final
iPhone  6SPlus  6SPlus
Samsung -      Samsung


I am trying a case statement like this but this isn't working.
CASE
       select Device,Model,length(Model) <3 then Device as Final
       else select Device,Model,Model as Final
END

Can someone please help here.

Advertisement

Answer

Read about CASE operator syntax.

select device, model, 
       case when length(model) <3 then Device else model end as Final
  from my_table;

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement