【问题标题】:Hive SQL: Select all rows before eventHive SQL:选择事件之前的所有行
【发布时间】:2018-08-06 17:05:21
【问题描述】:

在 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

【问题讨论】:

    标签: sql window hiveql row-number


    【解决方案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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-06
      • 2010-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      相关资源
      最近更新 更多