我正在计算Amazon Redshift中的月保留率,并提出以下问题:
Query 1

SELECT EXTRACT(year FROM activity.created_at) AS Year,
       EXTRACT(month FROM activity.created_at) AS Month,
       COUNT(DISTINCT activity.member_id) AS active_users,
       COUNT(DISTINCT future_activity.member_id) AS retained_users,
       COUNT(DISTINCT future_activity.member_id) / COUNT(DISTINCT activity.member_id)::float AS retention
FROM ads.fbs_page_view_staging activity
  LEFT JOIN ads.fbs_page_view_staging AS future_activity
         ON activity.mongo_id = future_activity.mongo_id
AND datediff ('month',activity.created_at,future_activity.created_at) = 1
GROUP BY Year,
         Month
ORDER BY Year,
         Month

出于某种原因,此查询返回zeroretained_userszeroretention。如果您能帮我解释一下为什么会发生这种情况,或者对每月保留金提出一个完全不同的查询,我将不胜感激。
我按照另一个SO post修改了查询,结果如下:
Query 2
WITH t AS (
   SELECT member_id
         ,date_trunc('month', created_at) AS month
         ,count(*) AS item_transactions
         ,lag(date_trunc('month', created_at)) OVER (PARTITION BY  member_id
                                           ORDER BY date_trunc('month', created_at))
          = date_trunc('month', created_at) - interval '1 month'
            OR NULL AS repeat_transaction
   FROM   ads.fbs_page_view_staging
   WHERE  created_at >= '2016-01-01'::date
   AND    created_at <  '2016-04-01'::date -- time range of interest.
   GROUP  BY 1, 2
   )
SELECT month
      ,sum(item_transactions) AS num_trans
      ,count(*) AS num_buyers
      ,count(repeat_transaction) AS repeat_buyers
      ,round(
          CASE WHEN sum(item_transactions) > 0
             THEN count(repeat_transaction) / sum(item_transactions) * 100
             ELSE 0
          END, 2) AS buyer_retention
FROM   t
GROUP  BY 1
ORDER  BY 1;

此查询提供以下错误:
An error occurred when executing the SQL command:
WITH t AS (
   SELECT member_id
         ,date_trunc('month', created_at) AS month
         ,count(*) AS item_transactions
         ,lag(date_trunc('m...

[Amazon](500310) Invalid operation: Interval values with month or year parts are not supported
Details:
 -----------------------------------------------
  error:  Interval values with month or year parts are not supported
  code:      8001
  context:   interval months: "1"
  query:     616822
  location:  cg_constmanager.cpp:145
  process:   padbmaster [pid=15116]
  -----------------------------------------------;

我觉得Query 2会比Query 1好,所以我更愿意修正这个错误。
任何帮助都将不胜感激。

最佳答案

问题1看起来不错。我也试过类似的。见下文。您正在使用表上的self join(ads.fbs_page_view_staging)和同一列(created_at)。假设mongo_id是唯一的,datediff('month'....)将始终返回0,datediff ('month',activity.created_at,future_activity.created_at) = 1将始终为false。

-- Count distinct events of join_col_id that have lapsed for one month.
SELECT count(distinct E.join_col_id) dist_ct
FROM public.fact_events E
JOIN public.dim_table Z
  ON E.join_col_id = Z.join_col_id
WHERE datediff('month', event_time, sysdate) = 1;

-- 2771654 -- dist_ct

关于postgresql - Amazon Redshift中的每月保留,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36414444/

10-11 03:38