题解 | #每个6/7级用户活跃情况#

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

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

select 
    a1.uid,
    a1.act_month_total,
    a2.act_days_2021,
    a3.act_days_2021_exam,
    a4.act_days_2021_question
from (   
-- 总活跃月份数
select
    t1.uid,
    count(distinct substr(t2.time,1,7)) act_month_total	
from (
    select uid from user_info where level = 6 or level = 7
) t1
left join (
    select
        uid,
        start_time `time`
    from exam_record
    union
    select
        uid,
        submit_time `time`
    from practice_record
) t2
on t1.uid = t2.uid
group by uid
) a1
inner join (
-- 2021年活跃天数
select
    t1.uid,
    count(distinct substr(t2.time,1,10)) act_days_2021	
from (
    select uid from user_info where level = 6 or level = 7
) t1
left join (
    select
        uid,
        start_time `time`
    from exam_record where start_time >= '2021-01-01 00:00:00' and start_time <= '2021-12-31 23:59:59'
    union
    select
        uid,
        submit_time `time`
    from practice_record where submit_time >= '2021-01-01 00:00:00' and submit_time <= '2021-12-31 23:59:59'
) t2
on t1.uid = t2.uid
group by uid
) a2
on a1.uid = a2.uid
inner join (
-- 2021年试卷作答活跃天数
select
    t1.uid,
    count(distinct substr(t2.time,1,10)) act_days_2021_exam	
from (
    select uid from user_info where level = 6 or level = 7
) t1
left join (
    select
        uid,
        start_time `time`
    from exam_record where start_time >= '2021-01-01 00:00:00' and start_time <= '2021-12-31 23:59:59'
) t2
on t1.uid = t2.uid
group by uid
) a3
on a1.uid = a3.uid
inner join (
-- 2021年答题活跃天数
select
    t1.uid,
    count(distinct substr(t2.time,1,10)) act_days_2021_question	
from (
    select uid from user_info where level = 6 or level = 7
) t1
left join (
    select
        uid,
        submit_time `time`
    from practice_record where submit_time >= '2021-01-01 00:00:00' and submit_time <= '2021-12-31 23:59:59'
) t2
on t1.uid = t2.uid
group by uid
) a4
on a1.uid = a4.uid
order by act_month_total desc, act_days_2021 desc ;

全部评论

相关推荐

11-06 10:58
已编辑
门头沟学院 嵌入式工程师
双非25想找富婆不想打工:哦,这该死的伦敦腔,我敢打赌,你简直是个天才,如果我有offer的话,我一定用offer狠狠的打在你的脸上
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务