我有以下查询:
SELECT floor(datediff(u.created_at, curdate()) / 1) AS days_ago,
count(DISTINCT u.id) AS "New Users in Cohort",
count(DISTINCT i.user_id) AS "Uniq Users who invited Cohort",
count(DISTINCT u.id) / count(DISTINCT i.user_id) AS "% who invite"
FROM users u
LEFT JOIN invitations i
ON u.id = i.user_id
WHERE u.onboarding_started_at IS NOT NULL
GROUP BY days_ago;
我正在尝试获取查询以返回以下内容:
+----------+-----------+--------------------------+--------------+
| days_ago | New Users | Unique Users who Invited | % who invite |
+----------+-----------+--------------------------+--------------+
| 1 | 20 | 10 | 50% |
+----------+-----------+--------------------------+--------------+
新用户将位于u.created_at是查询使用的日期
我在做什么错?谢谢!
答案 0 :(得分:0)
您还没说够我判断是否需要DISTINCT
。
LEFT
似乎不必要。
“昨天”是`...> = CURDATE()-间隔1天和... 如果您希望连续数天计数,那么我将使用SELECT count(DISTINCT u.id) AS "New Users in Cohort",
count(DISTINCT i.user_id) AS "Uniq Users who invited Cohort",
count(DISTINCT u.id) / count(DISTINCT i.user_id) AS "% who invite"
FROM users u
JOIN invitations i
ON u.id = i.user_id
WHERE u.onboarding_started_at IS NOT NULL
AND u.created_at >= CURDATE() - INTERVAL 1 DAY
AND u.created_at < CURDATE()
DATE(u.created_at)
作为DATE
的显示值和GROUPing BY
。 (除非您真的想要“ days_ago”。)