代码之家  ›  专栏  ›  技术社区  ›  Micah Pearce

配置单元SQL:选择事件之前的所有行

  •  0
  • Micah Pearce  · 技术社区  · 6 年前

    在蜂巢里,我有以下数据

    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
    

    如何为每个人和会话(包括第一个类型=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 回复  |  直到 6 年前
        1
  •  1
  •   Micah Pearce    6 年前

    可以使用窗口函数:

    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;