1.给出满足的条件,截止至2019-07-27的近30天
activity_date BETWEEN DATE_ADD('2019-07-27',INTERVAL -29 day) and '2019-07-27'
这里使用了Between and 函数和 Date_add函数
2.按照日期分组,统计活跃用户个数
select activity_date day,count(distinct(user_id)) active_users
from Activity
group by activity_date
最终SQL代码:
select activity_date day,count(distinct(user_id)) active_users
from Activity
where activity_date BETWEEN DATE_ADD('2019-07-27',INTERVAL -29 day) and '2019-07-27'
group by activity_date
LeetCode官方题解