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

Funnel query with Amazon Redshift / PostgreSQL

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

我想提取渠道统计信息。例如:

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

以上回答非常正确。我已经为将它用于 AWS Mobile Analytics 和 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 client_id,
         min(case when event_type = 'App Launch' then event_timestamp end) as ts_homepage,
         min(case when event_type = 'SignUp Success' then event_timestamp end) as ts_productpage,
         min(case when event_type = 'Start Quiz' then event_timestamp end) as ts_checkout
  from awsma.v_event
  group by client_id
 ) ts;

以防需要更精确的模型:当产品页面可以打开两次时。第一次在主页之前,第二次在主页之后。这种情况通常也应视为转换。

Redshift SQL 查询:

SELECT
COUNT(
 DISTINCT CASE WHEN cur_homepage_time IS NOT NULL
 THEN user_id END
) Step1,
COUNT(
DISTINCT CASE WHEN cur_homepage_time IS NOT NULL AND cur_productpage_time IS NOT NULL
  THEN user_id END
) Step2,
COUNT(
DISTINCT CASE WHEN
  cur_homepage_time IS NOT NULL AND cur_productpage_time IS NOT NULL AND cur_checkout_time IS NOT NULL
  THEN user_id END
) Step3
FROM (
   SELECT
     user_id,
     timestamp,
     COALESCE(homepage_time,
              LAG(homepage_time) IGNORE NULLS OVER(PARTITION BY user_id
              ORDER BY time)
     ) cur_homepage_time,
     COALESCE(productpage_time,
              LAG(productpage_time) IGNORE NULLS OVER(PARTITION BY distinct_id
              ORDER BY time)
     ) cur_productpage_time,
     COALESCE(checkout_time,
              LAG(checkout_time) IGNORE NULLS OVER(PARTITION BY distinct_id
              ORDER BY time)
     ) cur_checkout_time
   FROM
     (
       SELECT
         timestamp,
         user_id,
         (CASE WHEN event = 'homepage'
           THEN timestamp END) homepage_time,
         (CASE WHEN event = 'product page'
           THEN timestamp END) productpage_time,
         (CASE WHEN event = 'checkout'
           THEN timestamp END) checkout_time
       FROM events
       WHERE timestamp > '2016-05-01' AND timestamp < '2017-01-01'
       ORDER BY user_id, timestamp
     ) event_times
   ORDER BY user_id, timestamp
 ) event_windows

此查询用事件发生的最近时间戳填充每一行的 cur_homepage_timecur_productpage_timecur_checkout_time。因此,如果某个特定时间(读取行)事件发生,则特定列不是 NULL.

更多信息here