less than 1 minute read

Problem

problem-1141

Query

MySQL

SELECT
    activity_date AS day,
    COUNT(DISTINCT user_id) AS active_users
FROM
    Activity
GROUP BY
    activity_date
HAVING
    activity_date BETWEEN DATE_SUB('2019-07-28', INTERVAL 30 DAY) AND '2019-07-27';

T-SQL

SELECT
    activity_date AS day,
    COUNT(DISTINCT user_id) AS active_users
FROM
    Activity
WHERE
    activity_date BETWEEN DATEADD(DAY, -30, '2019-07-28') AND '2019-07-27'
GROUP BY
    activity_date;

Editorial Solution

SELECT 
    activity_date AS day, 
    COUNT(DISTINCT user_id) AS active_users
FROM 
    Activity
WHERE 
    DATEDIFF('2019-07-27', activity_date) < 30 AND DATEDIFF('2019-07-27', activity_date)>=0
GROUP BY 1

Tags:

Updated: