# 是否可以用sql语句实现分数的分段人数统计解决方法

oracle 专栏收录该内容
79 篇文章 0 订阅

------解决方案--------------------------------------------------------

select name,id,trunc(score/10),count(*) from student_score
group by name,id,trunc(score/10)

------解决方案--------------------------------------------------------
select trunc(score/10) , count(trunc(score/10))
from student_score
group by trunc(score/10)
order by 1

=========================================================

# 数据库查询-统计各科成绩各分数段人数：课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比解决方法

create table Student(S# varchar(10),Sname varchar(10),Sage date,Ssex varchar(10));
create table Course(C# varchar(10),Cname varchar(10),T# varchar(10));
create table Teacher(T# varchar(10),Tname varchar(10));
create table SC(S# varchar(10),C# varchar(10),score decimal(18,1));

select m.C# , m.Cname , (
case when n.score >= 85 then '85-100'
when n.score >= 70 and n.score < 85 then '70-85'
when n.score >= 60 and n.score < 70 then '60-70'
else '0-60'
end) as px,
count(1)
from Course m , sc n
where m.C# = n.C#
group by m.C# , m.Cname , (
case when n.score >= 85 then '85-100'
when n.score >= 70 and n.score < 85 then '70-85'
when n.score >= 60 and n.score < 70 then '60-70'
else '0-60'
end)
order by m.C# , m.Cname , px

------解决方案--------------------------------------------------------

------解决方案--------------------------------------------------------
count按 课程类别 和 分数阶段统计数量

------解决方案--------------------------------------------------------

1 01 language 70-85 4 66.67%
2 01 language 0-60 2 33.33%
3 02 maths 85-100 3 50%
4 02 maths 70-85 1 16.67%
5 02 maths 60-70 1 16.67%
6 02 maths 0-60 1 16.67%
7 03 english 85-100 2 33.33%
8 03 english 70-85 2 33.33%
9 03 english 0-60 2 33.33%
SQL code
select t1.*,round(t1.num/t2.all_num*100,2) || '%' 百分比
from
(select m.C# , m.Cname , (
case when n.score >= 85 then '85-100'
when n.score >= 70 and n.score < 85 then '70-85'
when n.score >= 60 and n.score < 70 then '60-70'
else '0-60'
end) as px,
count(1) num
from Course m , sc n
where m.C# = n.C#
group by m.C# , m.Cname , (
case when n.score >= 85 then '85-100'
when n.score >= 70 and n.score < 85 then '70-85'
when n.score >= 60 and n.score < 70 then '60-70'
else '0-60'
end)
order by m.C# , m.Cname , px) t1,

(select m.C# , m.Cname ,
count(1) all_num
from Course m , sc n
where m.C# = n.C#
group by m.C# , m.Cname
order by m.C# , m.Cname) t2
where t1.c#=t2.c#

===========================================================

# 编写存储过程，要求实现如下功能：输入课程名称，产生该课程各分数段及其相应人数的成绩分布情况统计。该怎么解决

-----解决方案--------------------------------------------------------

SQL code
---测试数据---
CREATE TABLE 表 (课程名 varchar(20),分数 int)
insert 表
select '语文',80 union all
select '语文',90 union all
select '语文',50 union all
select '语文',65 union all
select '数学',80 union all
select '数学',95 union all
select '数学',100 union all
select '数学',90

---定义存储过程---
if object_id('dbo.ScoreProc') is not null
drop proc dbo.ScoreProc
GO
Create proc dbo.ScoreProc @course varchar(50)
as
begin
select
课程名,
sum(case when 分数 between 0 and 59 then 1 else 0 end) as '60分以下',
sum(case when 分数 between 60 and 74 then 1 else 0 end) as '60-74分',
sum(case when 分数 between 75 and 84 then 1 else 0 end) as '75-84分',
sum(case when 分数 between 85 and 100 then 1 else 0 end) as '85-100分'
from 表
where 课程名=@course
group by 课程名
end

---调用存储过程---
exec ScoreProc '语文'
exec ScoreProc '数学'

---结果---
/**

-------------------- ----------- ----------- ----------- -----------

-------------------- ----------- ----------- ----------- -----------

**/

• 4
点赞
• 0
评论
• 8
收藏
• 扫一扫，分享海报

08-30 1404

11-02 81
08-14 2529
11-20 8063
05-30 287
12-22 227
01-09 417