日期:2014-05-18 浏览次数:20672 次
---测试数据---
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
**/