展开全部
--创建表
create table student
(
姓名 nvarchar(20),
数学32313133353236313431303231363533e58685e5aeb931333264663639 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)/a*100)+'%' 数学不及格率,
convert(nvarchar(20),cast(c as float)/a*100)+'%' 语文不及格率,
convert(nvarchar(20),cast(d as float)/a*100)+'%' 英语不及格率,
convert(nvarchar(20),cast(e as float)/a*100)+'%' 体育不及格率,
convert(nvarchar(20),cast(f as float)/a*100)+'%' 物理不及格率,
convert(nvarchar(20),cast(g as float)/a*100)+'%' 化学不及格率
from test
执行结果如图所示