with t2 as (
select t.*,t1.tag from exam_record t 
    inner join examination_info t1 using(exam_id) where year(start_time)=2021 )
    select uid,count(submit_time  is null or null) as incomplete_cnt,count(submit_time ) as complete_cnt
    ,group_concat(distinct concat(date(start_time),':',tag) order by start_time  separator ";") as detail from t2 
    group by uid having complete_cnt>=1 and incomplete_cnt between 2 and 4
    order by complete_cnt
根据思路进行
首先想到可能表连接会多次进行,所以使用的with 临时表。
根据每个用户的情况,可以自然想到groupby uid,筛选条件都是聚合后的,所以自然想到 having。
条件1:有效用户,指完成试卷作答数至少为1且未完成数小于5
条件2:未完成试卷作答数大于1结合得 1``<未完成试卷答题数<``5 , 完成试卷作答数 >= 1
条件3:考试开始年限为2021年
主要是第三个group_concat 的使用。
group_concat( DISTINCT 要连接的字段Order BY ASC/DESC 排序字段]Separator '分隔符')
易错点:
1、这题不能用score去判断是否为null,不然提交的时候,有的人有成绩没提交时间
2、group_concat 需要使用distinct,因为有重复的
3、最后要排序
[[分组计算]] [[group_concat]]

 京公网安备 11010502036488号
京公网安备 11010502036488号