sql - 使用 Amazon Redshift/PostgreSQL 进行漏斗查询

标签 sql analytics amazon-redshift

我正在尝试使用 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/

相关文章:

sql - 条件排名

amazon-s3 - Redshift 光谱 : Automatically partition tables by date/folder

amazon-redshift - 使用复制命令和 list 文件将 parquet 格式文件加载到 Amazon Redshift 时出错

postgresql - 对两个单独的列输出使用 LIKE

mysql - 如何使用WITH子句组合MYSQL查询

sql - 在 select 语句中设置参数

mysql - 我是否应该对表进行非规范化,是否应该对 FK 列进行非规范化

mysql - SQL查询以返回具有特定ID的所有项目

google-analytics - 如何跟踪我的 Greasemonkey 脚本有多少次自动升级下载?

swift - 'sharedApplication' 不可用 : not available on iOS (App Extension) for Segmentio/Analytics Pod