Hive SQL:在事件之前选择所有行

时间:2018-08-06 17:05:22

标签: sql window hiveql row-number

在Hive中,我有以下数据

sess,person,type,number
a   mary    I   1
a   mary    I   2
a   mary    V   3
a   mary    V   4
b   mary    I   1
b   mary    V   2
b   mary    C   3
a   john    I   1
a   john    I   2
a   john    V   3
a   john    V   4
b   john    I   1
b   john    V   2
b   john    C   3

如何为每个人和会话选择所有内容,包括第一个type = V?输出应该像

sess,person,type,number
    a   mary    I   1
    a   mary    I   2
    a   mary    V   3
    b   mary    I   1
    b   mary    V   2
    a   john    I   1
    a   john    I   2
    a   john    V   3
    b   john    I   1
    b   john    V   2

1 个答案:

答案 0 :(得分:1)

您可以使用窗口功能:

select t.*
from (select t.*,
             min(case when type = 'V' then number end) over (partition by session, person order by number) as min_aid
      from t
     ) t
where min_aid is null or number <= aid;