题解 | #大小写混乱时的筛选统计#
大小写混乱时的筛选统计
https://www.nowcoder.com/practice/81cb12931a604811ae69d332515c7734
跑出来的代码。分享思路。
题目要求:
(1)筛选出试卷作答数小于3的类别tag;
(2)输出exam_record中的tag,upper(tag)对应的answer_cnt。也可以理解为把(exam_record表中各类tag和answer_cnt)中的answer_cnt替换成upper(tag)对应的answer_cnt,并保持tag不变。
(2)可能比较难理解
思路是:
创建t,储存exam_record表中各类tag和answer_cnt,为什么examination_info left join exam_record,因为examination_info 表里的tag种类是完整的,也可能后面判断比如(tag的answer_cnt<>0,upper(tag)的answer_cnt不存在)
创建t1,储存①满足条件(answer_cnt<3 且 不全为大写字母)的tag ②把tag转大写,记为u_tag;
创建t2,从t中筛选出u_tag和answer_cnt;
最后连表,用t1 left join t2,因为t1中有tag(题目要求tag),条件answer_cnt<>0,剔除exam_record表没有的tag
with t as ( select any_value(t.exam_id) as exam_id,tag,any_value(count(uid)) as answer_cnt from examination_info t left join exam_record t1 on t.exam_id=t1.exam_id group by tag) ,t1 as( select upper(tag) as u_tag,tag from t where answer_cnt<3 and tag<>upper(tag)) ,t2 as ( select tag,answer_cnt from t where tag in( select u_tag from t1 )) select t1.tag,answer_cnt from t1 left join t2 on t1.u_tag=t2.tag where answer_cnt <> 0