如何获取用户会话?

How to get user sessions?

我正在使用 redash,我需要获取用户行,其中日期字段之间的每一行增量都小于小时。

详细信息:我需要一个会话,用户 activity 其中有一些操作,会话结束由最后一个操作 + 1 小时定义。

用户行是 <id, action, date>

user_id     page    happened_at     
179,233 rooms.view.step.content  2017-03-01 09:24
179,233 rooms.view.step.content  2017-03-01 09:01
179,233 rooms.student-showcase   2017-03-01 12:02

datediff 应该在那里提供帮助,但在 redash - redshift 上不可用。

我正在寻找替代品。有人有想法吗?

请试试这个。您甚至可以选择 dateadd。

select id, action, date 
from users u1 
where exists 
      ( select 1 from users u2 
        where u1.id = u2.id 
        and u2.happened_at < (u1.happened_at + interval '1 hour') 
        and u2.happened_at > u1.happened_at )
union  
select id, action, date 
from users u1 
where exists 
      ( select 1 from users u2 
        where u1.id = u2.id 
        and u2.happened_at > (u1.happened_at + interval '1 hour') 
        and u2.happened_at < u1.happened_at )

顺便说一下,redshift 有 datediff。不确定为什么 redash 不支持它。