--创建表
create table student
(
姓名 nvarchar(20),
数学 float,
语文 float,
英语 float,
体育 float,
物理 float,
化学 float
)
go
--插入语句
insert into student values('枫',50,60,70,50,60,40)
insert into student values('巅',60,80,50,58,80,90)
go
--查询
select from student
go
--不及格科目数量
;with test as
(
(select 姓名,'数学' 科目,数学 成绩 from student where 数学<60) union all
(select 姓名,'语文' 科目,语文 成绩 from student where 语文<60) union all
(select 姓名,'英语' 科目,英语 成绩 from student where 英语<60) union all
(select 姓名,'体育' 科目,体育 成绩 from student where 体育<60) union all
(select 姓名,'物理' 科目,物理 成绩 from student where 物理<60) union all
(select 姓名,'化学' 科目,化学 成绩 from student where 化学<60)
)
select 姓名,count(姓名)不及格科目数量 from test group by 姓名
--不及格率
;with test as
(
select distinct
(select count(姓名) from student)a,
(select count(姓名) from student where 数学<60)b,
(select count(姓名) from student where 语文<60)c,
(select count(姓名) from student where 英语<60)d,
(select count(姓名) from student where 体育<60)e,
(select count(姓名) from student where 物理<60)f,
(select count(姓名) from student where 化学<60)g
from student
)
select
convert(nvarchar(20),cast(b as float)/a100)+'%' 数学不及格率,
convert(nvarchar(20),cast(c as float)/a100)+'%' 语文不及格率,
convert(nvarchar(20),cast(d as float)/a100)+'%' 英语不及格率,
convert(nvarchar(20),cast(e as float)/a100)+'%' 体育不及格率,
convert(nvarchar(20),cast(f as float)/a100)+'%' 物理不及格率,
convert(nvarchar(20),cast(g as float)/a100)+'%' 化学不及格率
from test
执行结果如图所示
SELECT DISTINCT(aid),CAST(cnum100/bnum AS VARCHAR)+'%'FROM A a LEFT JOIN (SELECT id,MAX(num) AS num FROM A GROUP BY id) b ON aid=bid
LEFT JOIN (SELECT id,min(num) AS num FROM A GROUP BY id) c ON aid=cid
或者
SELECT DISTINCT(aid),CAST(cnum100/bnum AS VARCHAR)+'%'
FROM A a LEFT JOIN (SELECT id,num AS num FROM A WHERE course='sum') b ON aid=bid
LEFT JOIN (SELECT id,num AS num FROM A WHERE course='pass') c ON aid=cidSELECT
区
,CONVERT(DECIMAL(15,3),SUM(CASE 及格WHEN 'Y' THEN 1 ELSE 0 END))/COUNT(1)100
FROM
表
GROUP BY
区
-------------输出结果-------------
A 10000000000000000
B 3333333333333300
C 5000000000000000select C_TEACH_NAME,(select count() as count0 from c,e,f where cC_TEACH_CODE=e C_TEACH_CODE and eC_CLASS_CODE=fC_CLASS_CODE group by C_TEACH_NAME),(select count() as count1,round(count1/count0,2) from c,e,f,g where cC_TEACH_CODE=e C_TEACH_CODE and eC_CLASS_CODE=fC_CLASS_CODE and fC_STUDENT_CODE=gC_STUDENT_CODE and N_STUDENT_SCORE>=60 group by C_TEACH_NAME) from c group by C_TEACH_NAME;SELECT
subject AS 科目,
SUM( CASE WHEN absent = 0 THEN 1 ELSE 0 END ) AS 参考人数,
SUM( CASE WHEN absent = 1 THEN 1 ELSE 0 END ) AS 缺考人数,
AVG( score ) AS 平均成绩,
SUM( CASE WHEN score < 60 THEN 1 ELSE 0 END ) AS 不及格人数,
100 SUM( CASE WHEN score > 60 THEN 1 ELSE 0 END ) / COUNT(CODE) AS 及格率,
SUM( CASE WHEN score >= 90 THEN 1 ELSE 0 END ) AS 优秀人数,
100 SUM( CASE WHEN score >= 90 THEN 1 ELSE 0 END ) / COUNT(CODE) AS 优秀率
FROM
score
GROUP BY
subject
注:
以上 SQL ,假设 absent缺考标识 = 0 为参考, absent缺考标识 = 1 为缺考
成绩大于等于 90 分的,认为是 优秀
及格率的计算公式为:及格率=通过规定科目考试学生数÷按规定应参加考试生数X100%。
这里的分母不包括因故休学等符合政策不能参加考试的学生数,但包括按规定可以免试的学生效,因此它与“实际参加考试学生数”不同。
之所以不用“实际参加考试学生数”做分母,是为了防止采取如剥夺估计不能通过考试的学生参加考试的权利等之类的不正当手段。
及格率基本上有两种:
一是“单科及格率”,即通过某个学习或训练科目考试学生数的比例;
二是“全科及格率”,即通过某个阶段教育规定的所有科目的考试学生数的比例。
因为一般情况下只有毕业年级的学生才有可能“全科合格”,所以它意味着学生达到了毕业标准。那么,“全科及格率”(即使学生提前全科合格)与“按时毕业率”是互证的。
及格率是评价教育、教学基本质量和教育投资基本效益的一个极重要的指标。大面积提高及格率尤其是“全科及格率”,是贯彻国家的教育方针、实施素质教育的重要目标和结果。
sql 查询最高分、最低分和平均分语句//我们要用就以学生成绩为实例吧
/
结构
学生表
Student(S#,Sname,Sage,Ssex) --S# 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
--2课程表
Course(C#,Cname,T#) --C# --课程编号,Cname 课程名称,T# 教师编号
/
查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
--及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
--方法1
select mC# [课程编号], mCname [课程名称],
max(nscore) [最高分],
min(nscore) [最低分],
cast(avg(nscore) as decimal(18,2)) [平均分],
cast((select count(1) from SC where C# = mC# and score >= 60)1000 / (select count(1) from SC where C# = mC#) as decimal(18,2)) [及格率(%)],
cast((select count(1) from SC where C# = mC# and score >= 70 and score < 80 )1000 / (select count(1) from SC where C# = mC#) as decimal(18,2)) [中等率(%)],
cast((select count(1) from SC where C# = mC# and score >= 80 and score < 90 )1000 / (select count(1) from SC where C# = mC#) as decimal(18,2)) [优良率(%)],
cast((select count(1) from SC where C# = mC# and score >= 90)1000 / (select count(1) from SC where C# = mC#) as decimal(18,2)) [优秀率(%)]
from Course m , SC n
where mC# = nC#
group by mC# , mCname
order by mC#
--方法2
select mC# [课程编号], mCname [课程名称],
(select max(score) from SC where C# = mC#) [最高分],
(select min(score) from SC where C# = mC#) [最低分],
(select cast(avg(score) as decimal(18,2)) from SC where C# = mC#) [平均分],
cast((select count(1) from SC where C# = mC# and score >= 60)1000 / (select count(1) from SC where C# = mC#) as decimal(18,2)) [及格率(%)],
cast((select count(1) from SC where C# = mC# and score >= 70 and score < 80 )1000 / (select count(1) from SC where C# = mC#) as decimal(18,2)) [中等率(%)],
cast((select count(1) from SC where C# = mC# and score >= 80 and score < 90 )1000 / (select count(1) from SC where C# = mC#) as decimal(18,2)) [优良率(%)],
cast((select count(1) from SC where C# = mC# and score >= 90)1000 / (select count(1) from SC where C# = mC#) as decimal(18,2)) [优秀率(%)]
from Course m
order by mC#
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)