FAQ: Common Metrics - 1 Day Retention 3

This community-built FAQ covers the “1 Day Retention 3” exercise from the lesson “Common Metrics”.

Paths and Courses
This exercise can be found in the following Codecademy content:

SQL: Analyzing Business Metrics

FAQs on the exercise 1 Day Retention 3

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Had trouble following this exercise’s solution and tried to do it my way instead, seems to be working too, even tho I wrote way more lines of code, so it’s less efficient I think. Just sharing in case someone might find it usefull.

WITH ret AS (
SELECT DATE(a.created_at) AS d1, COUNT(DISTINCT a.user_id) AS returning_users
FROM gameplays AS a
JOIN gameplays AS b
ON a.user_id = b.user_id
WHERE DATE(b.created_at) = DATE(d1,’+1 day’)
GROUP BY 1),
daily AS(
SELECT DATE(created_at) AS d1, COUNT(DISTINCT user_id) AS daily_users
FROM gameplays
GROUP BY 1)
SELECT ret.d1, ROUND (100 * returning_users / daily_users) AS day_retention
FROM ret
INNER JOIN daily
USING (d1)
GROUP BY 1;

Is there a reason why we use date(datetime(g2.created_at, ‘-1 day’))? Specifically, why we have “datetime” in there? Is this so we can add the modification ‘-1 day’? There isn’t an explanation, though I understand what is happening, as to why we need the datetime function. Thank you to anyone!

4 Likes

Why we must use “date(datetime(g2.created_at” instead of use “date(g2.created_at”?

Even i am unable to understand this modification date(datetime(g2.created_at, ‘-1 day’)
Is there any cheat sheet for this?? datetime with this -1 day

I’m not sure there’s a cheatsheet available for this but the best place to look is the SQLite docs (the -1 day syntax is covered under 3. Modifiers on this page)-
https://www.sqlite.org/lang_datefunc.html

My code below results in 0.0 for all records in the retention column, I tried a few different variations but I’m not able to get the desired result.

SELECT
date(g1.created_at) as dt
, count(distinct g1.user_id) as total_users
, count(distinct g2.user_id) as retained_users
, round((100 * (count(distinct g2.user_id) / count(distinct g1.user_id)))) as retention
FROM gameplays as g1
LEFT JOIN gameplays as g2 on
g1.user_id = g2.user_id
AND date(g1.created_at) = date(datetime(g2.created_at, ‘-1 day’))
group by 1
order by 1
limit 100;