由一条sql语句引发的学习之二
作者:互联网
select m.*,case
when m.max_num > n.maxvalue then '被考核' else '正常' end as maxvalue ,
nvl(n.CATEGORY,'其他') from (
select 'didi' x1 ,a.templateid ,b.template_name ,sum(a.num) send_num,count(1) user_num,max(a.num) max_num,trunc(sum(a.num)/count(1)) seg_num
from (
select phone_no, templateid, count(1)num
from math_score
where send_time > trunc(sysdate)
group by phone_no, templateid
having count(1) > $anum) a, school_info b
where a.templateid = b.template_id ${tiaojian}
group by a.templateid,b.template_name
union all
select 'didi',a.templateid,b.template_name ,sum(a.num) send_num,count(1) user_num,max(a.num) max_num,trunc(sum(a.num)/count(1)) seg_num
from (
select phone_no, templateid, count(1)num
from english_score
where send_time > trunc(sysdate)
group by phone_no, templateid
having count(1) > $anum) a, school_info b
where a.templateid = b.template_id ${tiaojian}
group by a.templateid,b.template_name
union all
select 'didi',a.templateid,b.template_name ,sum(a.num) send_num,count(1) user_num,max(a.num) max_num,trunc(sum(a.num)/count(1)) seg_num
from (
select phone_no, templateid, count(1)num
from chinese_score
where send_time > trunc(sysdate)
group by phone_no, templateid
having count(1) > $anum) a, school_info b
where a.templateid = b.template_id ${tiaojian}
group by a.templateid,b.template_name) m ,school_info n
where m.templateid = n.template_id
order by user_num desc;
场景:
求取一个学校内,每个(语文\数学\英语)老师所带学生的班级名、班级总分、班级人数、最高分,平均分、及格率,及格率是否符合正常情况(考核老师)
school_info表字段:老师id、班级、班级人数、及格率...
math_score表字段:老师id、班级、手机号、学生信息、数学成绩...
关键语法解析:
1、NVL函数:从两个表达式返回一个非 null 值。
2、case用法:详细解析
3、from+表名跟from+select语句作用一样。
4、Union和Union All区别:详解
Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;
Union All:对两个结果集进行并集操作,包括重复行,不进行排序;
标签:语句,count,sql,之二,num,template,templateid,where,select 来源: https://blog.csdn.net/m0_37806112/article/details/101096978