gpt4 book ai didi

postgresql - Amazon redshift 中的每月保留

转载 作者:行者123 更新时间:2023-11-29 13:24:26 26 4
gpt4 key购买 nike

我正在尝试计算 Amazon Redshift 中的每月保留率,并提出了以下查询:

查询 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

出于某种原因,此查询返回 zero retained_userszero retention。对于为什么会发生这种情况的任何帮助,或者对每月保留的完全不同的查询可能会起作用,我将不胜感激。

我根据另一个 SO 帖子修改了查询,这里是:

查询 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]
-----------------------------------------------;

我觉得查询 2 会比 查询 1 更好,所以我更愿意修复它的错误。

如有任何帮助,我们将不胜感激。

最佳答案

查询 1 看起来不错。我试过类似的。见下文。您在表 (ads.fbs_page_view_staging) 和同一列 (created_at) 上使用自联接。假设 mongo_id 是唯一的,datediff('month'....) 将始终返回 0 并且 datediff ('month',activity.created_at,future_activity.created_at) = 1 永远是错误的。

-- 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com