错了4次。每一次都是错在最后的结果。

逻辑不难,思路是先找出目标用户。再连两次表。

前三次错误:最后order by 的时候写错列。以及没考虑到两份题目均要在2021年的情况。 第四次错误:没考虑到可能有未完成。 最后通过结果:

#高难度、SQL、平均分大于80,用户等级为7,2021年有试卷完成记录的用户。
#上述用户 2021年,试卷总完成次数、题目总练习数。
#结果按试卷完成数升序,按题目练习数降序。
with a as (
select u.uid from user_info u 
left join  exam_record e1 on u.uid=e1.uid
left join  examination_info e2 on e1.exam_id=e2.exam_id
where tag = 'sql' and difficulty ='hard' and level = 7
group by u.uid having avg(score) > 80 )

select a.uid,
count(distinct if(e2.id is not null ,e2.id,null)) exam_cnt,
count(distinct if( p.id is not null ,p.id,null)) question_cnt from
a left join exam_record e2 on a.uid=e2.uid
left join practice_record p on a.uid=p.uid and p.submit_time like '2021%'
where e2.submit_time like '2021%'
group by a.uid
having exam_cnt > 0
order by exam_cnt,question_cnt desc