使用Hive中的case语句填充先前的值

时间:2018-09-25 18:46:53

标签: mysql sql hive hiveql hive-query

我在event表中有一个列Hive,如下所示。

Event

Sent
Sent
Open
Open
Click
Sent
Open
Signup
Sent
Open
Click

现在,我想使用case语句基于event列中的值创建新列。

我想在事件列中有signup的地方,我希望Previous_event列正好是preceding的值。

我尝试过如下操作。

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

我如何实现我想要的?

2 个答案:

答案 0 :(得分:2)

使用相关子查询。

基于OP's comments,您可以尝试以下查询(对于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;

答案 1 :(得分:1)

以下是包含类似问题/解决方案的URL:

Hive access previous row value

https://community.hortonworks.com/questions/39533/fill-null-with-previous-row-values-in-hive.html

SQL将为:

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

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