题解 | #统计新用户#

牛客每个人最近的登录日期(四)

http://www.nowcoder.com/practice/e524dc7450234395aa21c75303a42b0a

思路如下:
1.首先 通过两个group by 第一个groupby获得每个新用户的登陆时间
第二个groupby 获得每个时间的新用户个数 注:这里只会统计>0的,所以在后面还要用case取=0的
(select date,count(user_id) cnt from
(select user_id ,min(date) date from login group by user_id)
2.等于0的日期是前面没有获取到的,所以从login中获得,然后left join保留login中全部日期,对于没有新用户的用case 语句改为0

select distinct l.date,case when c.cnt is null then
0 else c.cnt end cnt from login l
left join
(select date,count(user_id) cnt from
(select user_id ,min(date) date from login group by user_id) t
group by date) c
on l.date = c.date

全部评论

相关推荐

挂的真快
投递小鹏汽车等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务