首页 > 解决方案 > 如何获取用户会话?

问题描述

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

更详细地说:我需要一个会话,用户活动,它有一些操作,会话结束由最后一个操作 + 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.

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

标签: sqlamazon-redshiftredash

解决方案


请试试这个。您甚至可以选择 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 不支持它。


推荐阅读