• SQL练习题20200903


    github SQL练习题20200903

    MySQL:只用一个sql select 语句查询有不及格科目的学生,不及格科目的数量,以及平均分(所有科目的平均分,分数分布在2张表中)。
    要点:1、统计每个学生不及格可科目的个数。2、计算平均分。

    create table students1(
    id int,
    name varchar(25),
    course varchar(25),
    score float
    );
    insert into students1 (id, name,course, score) values(1, 'zhangsan', 'math', 83);
    insert into students1 (id, name,course, score) values(2, 'zhangsan', 'chinese', 54);
    insert into students1 (id, name,course, score) values(3, 'zhangsan', 'eng', 59);
    insert into students1 (id, name,course, score) values(4, 'lisi', 'math', 56);
    insert into students1 (id, name,course, score) values(5, 'lisi', 'chinese', 57);
    insert into students1 (id, name,course, score) values(6, 'lisi', 'eng', 50);
    insert into students1 (id, name,course, score) values(7, 'wagnwu', 'math', 83);
    insert into students1 (id, name,course, score) values(8, 'wagnwu', 'chinese', 100);
    insert into students1 (id, name,course, score) values(9, 'wagnwu', 'chinese', 100);
    
    create table students2(
    id int,
    name varchar(25),
    course varchar(25),
    score float
    );
    insert into students2 (id, name,course, score) values(1, 'zhangsan', 'physics', 60);
    insert into students2 (id, name,course, score) values(2, 'zhangsan', 'chemistry', 49);
    insert into students2 (id, name,course, score) values(3, 'zhangsan', 'biology', 90);
    insert into students2 (id, name,course, score) values(4, 'lisi', 'physics', 59);
    insert into students2 (id, name,course, score) values(5, 'lisi', 'biology', 60);
    insert into students2 (id, name,course, score) values(6, 'lisi', 'biology', 100);
    insert into students2 (id, name,course, score) values(7, 'wagnwu', 'physics', 90);
    insert into students2 (id, name,course, score) values(8, 'wagnwu', 'biology', 45);
    
    

    首先,将两个表的数据合并起来,使用union all,然后基于合并的数据根据学生名进行分组,使用聚合函数avg计算出平均分,然后使用case when来判断score不及格的数量(这里使用sum结合case when进行统计),使用having进行条件过滤,将全部含有不及格成绩的学生列出来。

    SELECT name,avg(score), sum(case when score<60 then 1 else 0 end) FROM((SELECT * FROM students1) UNION ALL (SELECT * FROM students2 )) as a group by name having sum(case when score <60 then 1 else 0 end)>1;
    +----------+--------------------+-------------------------------------------+
    | name     | avg(score)         | sum(case when score<60 then 1 else 0 end) |
    +----------+--------------------+-------------------------------------------+
    | zhangsan |  65.83333333333333 |                                         3 |
    | lisi     | 63.666666666666664 |                                         4 |
    +----------+--------------------+-------------------------------------------+
    
  • 相关阅读:
    用stetho通过网页访问手机数据库
    Python学习笔记(一)
    Linux学习笔记(四)Linux常用命令
    Linux学习笔记(三)Shell命令机制
    Linux学习笔记(一)
    Linux学习笔记(五)Linux应用程序的安装和卸载
    Linux学习笔记(二)Shell教程
    Linux学习笔记(六)Linux服务程序的安装和卸载
    前端模块总结
    div+css命名规范
  • 原文地址:https://www.cnblogs.com/liangping/p/13611139.html
Copyright © 2020-2023  润新知