题目描述
https://www.nowcoder.com/practice/a32c7c8590324c96950417c57fa6ecd1
思路: 请统计每个6/7级用户总活跃月份数、2021年活跃天数、2021年试卷作答活跃天数、2021年答题活跃天数,按照总活跃月份数、2021年活跃天数降序排序。
本题的难度在于要查找的东西太多了,很难一步写出来,可以先根据每个要求写出对应的sql最后在连接到一个大表里即可
1.先找到6/7级用户
select uid from user_info where level=6 or level=7
2.总活跃月份数
select u.uid,count(distinct date_format(submit_time,"%Y%m")) as act_month_total
from user_info as u left join (
select uid,submit_time from exam_record
union all
select uid,submit_time from practice_record
) as a on u.uid=a.uid
where u.uid in (select uid from user_info where level=6 or level=7)
group by u.uid
order by act_month_total desc
3.2021年活跃天数
select u.uid,count(distinct if(year(date_format(submit_time,"%Y%m%d"))=2021,(date_format(submit_time,"%Y%m%d")),null)) as act_days_2021
from user_info as u left join(
select uid,submit_time from exam_record
union all
select uid,submit_time from practice_record
) as a on u.uid=a.uid
where u.uid in (select uid from user_info where level=6 or level=7)
group by u.uid
order by act_days_2021 desc
4.2021年试卷作答活跃天数
select u.uid,count(distinct if(year(date_format(submit_time,"%Y%m%d"))=2021,(date_format(submit_time,"%Y%m%d")),null)) as act_days_2021_exam
from user_info as u left join exam_record as r on u.uid=r.uid
where u.uid in (select uid from user_info where level=6 or level=7)
group by u.uid
5.2021年答题活跃天数
select u.uid,count(distinct if(year(date_format(submit_time,"%Y%m%d"))=2021,(date_format(submit_time,"%Y%m%d")),null)) as act_days_2021_question
from user_info as u left join practice_record as p on u.uid=p.uid
where u.uid in (select uid from user_info where level=6 or level=7)
group by u.uid
代码合并,使用left join进行连接,但是因为我们在每一个小块里面已经是left join了所以最后大表链接的时候也可以直接用join
# 请统计每个6/7级用户总活跃月份数、2021年活跃天数、2021年试卷作答活跃天数、2021年答题活跃天数,
# 按照总活跃月份数、2021年活跃天数降序排序。
select u.uid,one.act_month_total,two.act_days_2021,three.act_days_2021_exam,four.act_days_2021_question
from user_info as u join(
select u.uid,count(distinct date_format(submit_time,"%Y%m")) as act_month_total
from user_info as u left join (
select uid,submit_time from exam_record
union all
select uid,submit_time from practice_record
) as a on u.uid=a.uid
where u.uid in (select uid from user_info where level=6 or level=7)
group by u.uid
) as one on u.uid=one.uid
left join(
select u.uid,count(distinct if(year(date_format(submit_time,"%Y%m%d"))=2021,(date_format(submit_time,"%Y%m%d")),null)) as act_days_2021
from user_info as u left join(
select uid,submit_time from exam_record
union all
select uid,submit_time from practice_record
) as a on u.uid=a.uid
where u.uid in (select uid from user_info where level=6 or level=7)
group by u.uid
)as two on u.uid=two.uid
left join(
select u.uid,count(distinct if(year(date_format(submit_time,"%Y%m%d"))=2021,(date_format(submit_time,"%Y%m%d")),null)) as act_days_2021_exam
from user_info as u left join exam_record as r on u.uid=r.uid
where u.uid in (select uid from user_info where level=6 or level=7)
group by u.uid
)as three on u.uid=three.uid
left join(
select u.uid,count(distinct if(year(date_format(submit_time,"%Y%m%d"))=2021,(date_format(submit_time,"%Y%m%d")),null)) as act_days_2021_question
from user_info as u left join practice_record as p on u.uid=p.uid
where u.uid in (select uid from user_info where level=6 or level=7)
group by u.uid
)as four on u.uid=four.uid
order by one.act_month_total desc,two.act_days_2021 desc