我正在尝试计算过去9个月的每月红移用户保留队列。但是我遇到了一个问题,即以下查询中的月份队列未汇总到正确的月份中。
我要查询的数据类型是:
userid-varchar 激活的varchar
这是我要运行的查询:
with by_month as
(SELECT
userid
DATE_TRUNC('month', cast ("activated" as date)) AS joined_month
FROM customers
GROUP BY 1, 2),
first_month as
(select userid,
joined_month,
FIRST_VALUE(order_month) OVER (PARTITION BY userid ORDER BY
joined_month asc rows unbounded preceding) AS first
FROM by_month),
months as (select userid,
joined_month,
first,
extract(month from (joined_month - first_month)) as month_number
from first_month)
SELECT
first as "cohort",
SUM(CASE WHEN month_number = '0' THEN 1 ELSE 0 END) AS " Month 0",
SUM(CASE WHEN month_number = '1' THEN 1 ELSE 0 END) AS " Month 1",
SUM(CASE WHEN month_number = '2' THEN 1 ELSE 0 END) AS " Month 2",
SUM(CASE WHEN month_number = '3' THEN 1 ELSE 0 END) AS " Month 3",
SUM(CASE WHEN month_number = '4' THEN 1 ELSE 0 END) AS " Month 4",
SUM(CASE WHEN month_number = '5' THEN 1 ELSE 0 END) AS " Month 5",
SUM(CASE WHEN month_number = '6' THEN 1 ELSE 0 END) AS " Month 6",
SUM(CASE WHEN month_number = '7' THEN 1 ELSE 0 END) AS " Month 7",
SUM(CASE WHEN month_number = '8' THEN 1 ELSE 0 END) AS " Month 8",
SUM(CASE WHEN month_number = '9' THEN 1 ELSE 0 END) AS " Month 9"
from months
where first_month >= '2018-08-01'
GROUP BY 1
ORDER BY 1 desc
当我得到结果时,我得到了两个队列的不可能的数字:
例如:
Cohort Month 0 Month 1
'2019-01-01' 95 120
我进行了一些挖掘,发现月数未正确计算。例如,对于“ 2019-01-01”队列,month_ number是t正确捕获了0.1和3,但是2却被遗漏了-归因于第1个月。谢谢您提供有关修复的任何帮助!
答案 0 :(得分:0)
现在,尝试
SELECT userid, joined_month, first_month, month_number FROM months
WHERE first = '2019-01-01'
(随意添加其他列以深入研究问题)添加激活的,order_month等,直到您正确地了解引起问题的原因为止。