题解 | #每月及截止当月的答题情况#

每月及截止当月的答题情况

http://www.nowcoder.com/practice/1ce93d5cec5c4243930fc5e8efaaca1e


/*思路:先找出各个月份新增用户
  1.先求出各个用户的最早活动月份,作为子循环
  2.再在外部循环计算出各个月份出现的用户数极为每月新增用户
  可能会出现有些月份无新增,所以不会体现出来,这个时候需要用到左连接,计算出月活数
  */
select c.start_month
,c.mau
,coalesce(mon_add_cnt,0)
,max(mon_add_cnt) over(order by start_month) as max_month_add_uv
,sum(mon_add_cnt) over(order by start_month) as cum_sum_uv
from
(
    select date_format(start_time,'%Y%m') as start_month
           ,count(distinct uid) as mau
    from exam_record
    group by start_month
)c
left join 
  (
  select min_mon,count(a.uid) as mon_add_cnt
  from(
      select uid, date_format(min(start_time),'%Y%m') as min_mon
      from exam_record 
      group by uid
      )a
  group by min_mon
   )b
   on c.start_month = b.min_mon


数据库刷题题解 文章被收录于专栏

数据分析数据库题目练习题解

全部评论

相关推荐

神哥不得了:神哥来啦~专业技能的话建议不要前面空那么多,八股的话建议先把高频top 50的八股多巩固几遍,千万不要看那些假高频八股。项目的话,建议换两个高质量的项目上去
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务