题解 | #每个6/7级用户活跃情况#月份和日期区分开,没有记录显示0所以用左连接

每个6/7级用户活跃情况

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

select t.uid,count(distinct month) as act_month_total
       ,count(distinct if(year(day) = 2021,day,null)) as act_days_2021 
       ,count(distinct if(year(day) = 2021 and flag = 'exam',day,null)) as act_days_2021_exam 
       ,count(distinct if(year(day) = 2021 and flag = 'practice',day,null)) as act_days_2021_question 
from (
    select t1.uid,t2.month,t2.day,t2.flag
    from (select uid from user_info where level in (6,7)) t1
    left join(
            select uid,date_format(start_time,'%Y%m') as month,
                   date_format(start_time,'%Y%m%d') as day,
                   'exam'                  as flag
            from exam_record
            union
            select uid,date_format(submit_time,'%Y%m') as month,
                   date_format(submit_time,'%Y%m%d') as day,
                   'practice'             as flag
            from practice_record
            ) t2
    on t1.uid = t2.uid
        ) t
group by uid
order by act_month_total desc,act_days_2021 desc



全部评论

相关推荐

牛客41406533...:回答他在课上学,一辈子待在学校的老教授用三十年前的祖传PPT一字一句的讲解,使用谭浩强红皮书作为教材在devc++里面敲出a+++++a的瞬间爆出114514个编译错误来学这样才显得专业
点赞 评论 收藏
分享
11-07 16:07
深圳大学 运营
前端飞升:学长,阿里不是卡双非吗,我深也能去吗
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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