SQL problem - The Number of Employees Which Report to Each Employee
Problem
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