这道题本身挺简单,但是这个月均完成数的定义很模糊。这道题里的定义是 所有完成数/有完成题的月数。
首先是用subquery将月均完成数大于等于3的用户找出来,然后通过join将exam_record和examination连接并限制uid为月均完成数大于等于3的用户。
然后根据tag分组,select出tag并用count算出每组作答数量。最后根据作答数量倒序排序。
select ei.tag, count(er.start_time) as tag_cnt
from
exam_record as er
inner join
(select uid from exam_record
group by uid
having count(submit_time) / count(distinct date_format(submit_time, "%Y-%m")) >= 3) as user
using(uid)
left join examination_info as ei
using(exam_id)
group by ei.tag
order by tag_cnt desc