Search code examples
mysqlsqlhivehiveqlhive-query

Populate preceding value using case statement in Hive


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?


Solution

  • 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 :

    select event, prev_event(event) as Previous_event from table;
    

    The code for the UDF :

        import org.apache.hadoop.hive.ql.exec.UDF;
    
        public class cum_mul extends UDF  {
        private String prevValue = null;
    
        public String evaluate(String value) {
    
        switch(value) 
            { 
                case "Sent": 
            prevValue = "No_event";
                    return "No_event"; 
                case "Open": 
            prevValue = "Sent";
                    return "Sent";  
                case "Click": 
            prevValue = "Open";
                    return "Open"; 
                default: 
                    return prevValue; 
            } 
          }
    }