自学内容网 自学内容网

【牛客】SQL34 统计复旦用户8月练题情况

描述

题目: 现在运营想要了解复旦大学的每个用户在8月份练习的总题目数和回答正确的题目数情况,请取出相应明细数据,对于在8月份没有练习过的用户,答题数结果返回0.

示例:用户信息表user_profile

iddevice_idgenderageuniversitygpaactive_days_within_30
12138male21北京大学3.47
23214male复旦大学4.015
36543female20北京大学3.212
42315female23浙江大学3.65
55432male25山东大学3.820
62131male28山东大学3.315
74321female26复旦大学3.69

示例:question_practice_detail

iddevice_idquestion_idresultdate
12138111wrong2021-05-03
23214112wrong2021-05-09
33214113wrong2021-06-15
46543111right2021-08-13
52315115right2021-08-13
62315116right2021-08-14
72315117wrong2021-08-15
……

根据示例,你的查询应返回以下结果:

device_iduniversityquestion_cntright_question_cnt
3214复旦大学30
4321复旦大学00
with cte as(
    select
    device_id,university,question_id,result,date
    from
    user_profile left join question_practice_detail
    using(device_id)
    where university='复旦大学' 
    and (left(date,7)='2021-08' or date is null)
)

select
device_id,university,
ifnull(count(question_id),0) as question_cnt,
ifnull(sum(if(result='right',1,0)),0) as right_question_cnt
from cte
group by device_id;

 


原文地址:https://blog.csdn.net/weixin_60200880/article/details/136298984

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!