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

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

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

SELECT uid,
			count(DISTINCT stm) act_month_total,
			count(DISTINCT if(left(std,4)='2021',std,null)) act_days_2021,
			count(distinct if(left(std,4)='2021' and type='exam',std,null)) act_days_2021_exam,
			count(distinct if(left(std,4)='2021' and type='practice',std,null)) act_days_2021_question					
from 
(		SELECT ui.uid,t1.uid uid2,t1.ans_id,t1.stm,t1.std,t1.type from user_info ui left join (
		SELECT uid,exam_id as ans_id, DATE_FORMAT(submit_time,"%Y%m") as stm,DATE_FORMAT(submit_time,"%Y%m%d") as std,'exam' as type from exam_record
		union all
		SELECT uid,question_id as ans_id,DATE_FORMAT(submit_time,"%Y%m") as stm,DATE_FORMAT(submit_time,"%Y%m%d") as std,'practice' as type from practice_record
		)t1 on ui.uid=t1.uid
where level>=6
)t2
group by uid order by act_month_total desc,act_days_2021 desc;

这题目实在是有难度,不过学到了怎么统计条件为2021年的行数;
解题思路是自己造一个表,因为需要统计总的活跃天数,又需要统计考试和练习的活跃天数,因此表中要有一列能区分这二者。

全部评论
这个为什么需要ans_id这一列呀
点赞 回复 分享
发布于 2023-09-20 16:46 四川

相关推荐

球球别再泡了:坏,我单9要了14
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务