Populate preceding value using case statement in Hive

1.8k Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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; 
        } 
      }
}
2
On

Use a correlated subquery.

Based on OP's comments, you can try the following query (for MySQL):

select t1.event, 
       case when t1.event = 'Sent' then 'No_event' 
            when t1.event = 'Open' then 'Sent' 
            when t1.event = 'Click' then 'Open' 
            when t1.event = 'Signup' then (select t2.event 
                                           from table as t2 
                                           where t2.eventdate < t1.eventdate 
                                           order by t2.eventdate desc 
                                           limit 1)         
            else -1 
       end as Previous_event 
from table as t1;