1、建立学生表student(id uniqueidentifier PRIMARY DEFAULT (newid()),xh int,name varchar(50),sex bit,birthday datetime,memo varchar(4000))。
2、建立课程表 class(id uniqueidentifier PRIMARY DEFAULT (newid()),x h int,name varchar(50),sorce numeric(18,2))。
3、建立选课表 SC(uniqueidentifier PRIMARY DEFAULT (newid()),sid uniqueidentifier,cid uniqueidentifier,grade numeric(18,2),sum_grade numeric(18,2))其中sid、cid分别是student.id、class.id的外键。实现:统计每门课各个分数段的人数(以sc表的grade字段计算。以上是表结构的定义和要实现的问题(我的问题),希望各位大侠多多照顾,不吝答疑
2、建立课程表 class(id uniqueidentifier PRIMARY DEFAULT (newid()),x h int,name varchar(50),sorce numeric(18,2))。
3、建立选课表 SC(uniqueidentifier PRIMARY DEFAULT (newid()),sid uniqueidentifier,cid uniqueidentifier,grade numeric(18,2),sum_grade numeric(18,2))其中sid、cid分别是student.id、class.id的外键。实现:统计每门课各个分数段的人数(以sc表的grade字段计算。以上是表结构的定义和要实现的问题(我的问题),希望各位大侠多多照顾,不吝答疑
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 '数学'---结果---
/**
课程名 60分以下 60-74分 75-84分 85-100分
-------------------- ----------- ----------- ----------- -----------
语文 1 1 1 1课程名 60分以下 60-74分 75-84分 85-100分
-------------------- ----------- ----------- ----------- -----------
数学 0 0 1 3
**/
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 表
group by 课程名