I have a column event in Hive table like below.
Event
Sent
Sent
Open
Open
Click
Sent
Open
Signup
Sent
Open
Click
Now I want to create new column based on the values in event column using case statement.
I want to where there is signup in event column I want the Previous_event column to be exactly the preceding value.
I have tried like below.
select event, 
       case when event = 'Sent' then 'No_event' 
            when event = 'Open' then 'Sent' 
            when event = 'Click' then 'Open'  
            else -1 
       end as Previous_event 
from table;
Result 
Sent    No_event
Sent    No_event
Open    Sent
Open    Sent
Click   Open
Sent    No_event
Open    Sent
Signup  -1
Sent    No_event
Open    Sent
Click   Open
Expected result
Sent    No_event
Sent    No_event
Open    Sent
Open    Sent
Click   Open
Sent    No_event
Open    Sent
Signup  Open
Sent    No_event
Open    Sent
Click   Open
How can i achieve what I want?
                        
Below are the URLs that contain similar Problem/Solution:
Hive access previous row value
https://community.hortonworks.com/questions/39533/fill-null-with-previous-row-values-in-hive.html
The SQL will be :
The code for the UDF :