我正在尝试使用 Redshift 中的事件数据分析漏斗,但很难找到有效的查询来提取该数据。
例如,在 Redshift 中我有:
timestamp action user id
--------- ------ -------
2015-05-05 12:00 homepage 1
2015-05-05 12:01 product page 1
2015-05-05 12:02 homepage 2
2015-05-05 12:03 checkout 1
我想提取 channel 统计信息。例如:
homepage_count product_page_count checkout_count
-------------- ------------------ --------------
100 50 25
homepage_count
代表访问首页的不同用户数量,product_page_count
代表访问首页之后 访问首页的不同用户数量主页,checkout_count
表示访问主页和产品页面后结帐的用户数。
使用 Amazon Redshift 实现该目标的最佳查询是什么?是否可以使用单个查询?
最佳答案
我认为最好的方法可能是为每个用户第一次访问每种类型的数据添加标志,然后将这些用于聚合逻辑:
select sum(case when ts_homepage is not null then 1 else 0 end) as homepage_count,
sum(case when ts_productpage > ts_homepage then 1 else 0 end) as productpage_count,
sum(case when ts_checkout > ts.productpage and ts.productpage > ts.homepage then 1 else 0 end) as checkout_count
from (select userid,
min(case when action = 'homepage' then timestamp end) as ts_homepage,
min(case when action = 'product page' then timestamp end) as ts_productpage,
min(case when action = 'checkout' then timestamp end) as ts_checkout
from table t
group by userid
) t
关于sql - 使用 Amazon Redshift/PostgreSQL 进行漏斗查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30694659/