选出30天,每天的活跃用户数目。
解法一:两层嵌套
# Write your MySQL query statement below
select
*
from
(select
activity_date as day, count(distinct user_id) as active_users
from
activity
group by
activity_date
) db0
where
datediff('2019-07-27', day) < 30
and datediff('2019-07-27', day) >= 0
and active_users > 0
想法很简单,我们先group by,第二层用where选出count > 0并且日期符合要求的组,但其实是没有这个必要的,因为count = 0的行在group by时根本不会被提取出来。所以可以用下面的单层嵌套解决。
解法二:单层select
重命名可以不用as,直接命就行,nice。
# Write your MySQL query statement below
select
activity_date day, count(distinct user_id) active_users
from
activity
where
datediff('2019-07-27', activity_date) >= 0
and datediff('2019-07-27', activity_date) < 30
group by
activity_date