读书人

编写存储过程要求实现如下功能:输入

发布时间: 2012-06-08 12:55:24 作者: rapoo

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

[解决办法]
接楼上的

SQL code
---测试数据---CREATE TABLE 表 (课程名 varchar(20),分数 int)insert 表select '语文',80 union allselect '语文',90 union allselect '语文',50 union allselect '语文',65 union allselect '数学',80 union allselect '数学',95 union allselect '数学',100 union allselect '数学',90 ---定义存储过程---if object_id('dbo.ScoreProc') is not nulldrop proc dbo.ScoreProcGOCreate 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**/ 

读书人网 >SQL Server

热点推荐