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

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

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

select u.uid,count(distinct date_format(act_time,'%Y%m')) act_month_total,
count(distinct if(year(act_time)=2021,date(act_time),null)) act_days_2021,
count(distinct if(year(act_time)=2021 and tag='exam',date(act_time),null)) act_days_2021_exam,
count(distinct if(year(act_time)=2021 and tag='question',date(act_time),null)) act_days_2021_question
from user_info u
left join (
select uid,date(start_time) act_time,'exam' tag
from exam_record
union 
select uid,date(submit_time) act_tie,'question' tag
from practice_record) un
on u.uid=un.uid
where u.level in(6,7)
group by u.uid
order by act_month_total desc,act_days_2021 desc;

1.为了解决 完成试卷的活跃时间和练题的活跃时间,创建新表 将两个活跃时间都合成一列,并新增列tag以区分是exam还是question 这样的话 act_time一列就是年总活跃天数(试卷活跃天数+做题活跃天数)

select uid,date(start_time) act_time,‘exam’ tag

from exam_record

union

select uid,date(submit_time) act_time,'question' tag

2.用 count(distinct date_format(act_time,'%Y%m')) 来计算年总活跃月数(活跃月数相同要去重)

用 count(distinct if(year=2021,date(act_time),null) 来计算2021年活跃的总天数

用 count(distinct if(year=2021 and tag='exam',date(act_time),null))来计算2021年试卷活跃的总天数

用count(distinct if(year=2021 and tag='question',date(act_time),null))来计算2021年练题活跃的总天数

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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