题解 | #未完成试卷数大于1的有效用户#

未完成试卷数大于1的有效用户

http://www.nowcoder.com/practice/46cb7a33f7204f3ba7f6536d2fc04286

知识点:行转列运用的group_concat()函数,可以将多行拼接为列函数group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator'分隔符']),默认是“,”分割

第一遍写的时候犯了两个错误:1)没有对case when 后的结果进行正确聚合,然后还是sum是总计,count是计数会把包括0的一起归纳进去 2)应该先对start_time,';',tag进行拼接再用group_concat进行行转列的拼接,没认真读结果要求的格式只记得行转列的拼接了 另外的坑:where;group by;having的正确排序

sum(case when er.submit_time is null then 1 else 0 end)as imcompelete_cnt,
sum(case when er.submit_time is not null then 1 else 0 end)as complete_cnt,
group_concat(distinct concat(date_format(er.start_time,'%Y-%m-%d'),':',e.tag) order by er.start_time separator';')as detail
from exam_record er
left join examination_info e 
on er.exam_id=e.exam_id
where year(er.start_time)=2021
group by er.uid
having imcompelete_cnt>1 and complete_cnt>=1 and imcompelete_cnt<5
order by imcompelete_cnt desc
全部评论

相关推荐

2 收藏 评论
分享
牛客网
牛客企业服务