mysql统计不及格科目_SQL语句 统计各个学生的不及格科目数量及不及格率 ,2条语句...

本文介绍了如何使用SQL语句在MySQL中统计学生不及格的科目数量以及各科目的不及格率。通过创建student表并插入数据,然后使用CTE(公共表表达式)进行查询,分别计算每个学生的不及格科目数量和各科目不及格率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

展开全部

--创建表

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

执行结果如图所示

a662b707e2256ccc8037047e1c58269d.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值