--测试--测试数据 create table 表(学号 char(4),课程名称 varchar(10),成绩 int) insert 表 select '0001','语文',89 union all select '0002','数学',90 union all select '0001','英语',49 union all select '0002','历史',55 union all select '0003','英语',60 go--查询 select 课程名称 ,及格率=cast(cast(sum(case when 成绩<60 then 0.0 else 100.0 end) /count(*) as decimal(10,2)) as varchar)+'%' from 表 group by 课程名称 go--删除测试 drop table 表/*--测试结果 课程名称 及格率 ---------- --------------- 历史 0.00% 数学 100.00% 英语 50.00% 语文 100.00%(所影响的行数为 4 行) --*/
create table 表(学号 char(4),课程名称 varchar(10),成绩 int)
insert 表 select '0001','语文',89
union all select '0002','数学',90
union all select '0001','英语',49
union all select '0002','历史',55
union all select '0003','英语',60
go--查询
select 课程名称
,及格率=cast(cast(sum(case when 成绩<60 then 0.0 else 100.0 end)
/count(*) as decimal(10,2)) as varchar)+'%'
from 表
group by 课程名称
go--删除测试
drop table 表/*--测试结果
课程名称 及格率
---------- ---------------
历史 0.00%
数学 100.00%
英语 50.00%
语文 100.00%(所影响的行数为 4 行)
--*/
up !我拜你为师好了