会话的SQL汇总

时间:2018-02-28 03:01:59

标签: sql amazon-redshift

我有一个展示事件表,其中包含一堆时间戳和标记的开始/结束边界。我正在尝试将其推广到指标“此会话包含至少1次展示功能x”。我不确定如何做到这一点。任何帮助,将不胜感激。感谢。

enter image description here

我想把它推到看起来像这样的东西:

account, session_start, session_end, interacted_with_feature
3004514, 2018-02-23 13:43:35.475, 2018-02-23 13:43:47.377, FALSE

我很容易说这个会话是否与该功能有任何交互。

2 个答案:

答案 0 :(得分:1)

也许聚合可以做你想要的:

select account, min(timestamp), max(timestamp), max(interacted_with_feature)
from t
group by account;

答案 1 :(得分:0)

我能够通过条件累积和来解决这个问题,为每一行生成一个会话组ID。

with cte as (
    select *
        , sum(case when session_boundary = 'start' then 1 else 0 end) 
              over (partition by account order by timestamp rows unbounded preceding)
          as session_num
    from raw_sessions
)

select account
    , session_num
    , min(timestamp) as session_start
    , max(timestamp) as session_end
    , bool_or(interacted_with_feature) as interacted_with_feature
from cte
group by account, session_num