select '0.0-1.9' as 分数段,
sum(case when 分数>=0 and 分数<2 then 1 else 0 end) as 人数
union
select '2.0-3.9' as 分数段,
sum(case when 分数>=2 and 分数<4 then 1 else 0 end) as 人数
.....union
select '8.0-10.0' as 分数段,
sum(case when 分数>=8 and 分数<10 then 1 else 0 end) as 人数
sum(case when 分数>=0 and 分数<2 then 1 else 0 end) as 人数
union
select '2.0-3.9' as 分数段,
sum(case when 分数>=2 and 分数<4 then 1 else 0 end) as 人数
.....union
select '8.0-10.0' as 分数段,
sum(case when 分数>=8 and 分数<10 then 1 else 0 end) as 人数
create table 成绩表(姓名 varchar(10),成绩 decimal(10,1))
insert 成绩表 select 'aa',10
union all select 'bb',9
union all select 'cc',9
union all select 'dd',8
union all select 'ee',5
union all select 'ff',6
union all select 'gb',2
union all select 'hh',8
union all select 'ii',1
go--处理参数定义
declare @min int,@max int,@sectnum int
select @min=0,@max=10,@sectnum=5--开始处理
--创建处理的临时表
declare @i int
set rowcount @sectnum
select id=identity(int,0,1),a=cast(0 as decimal(10,1)),b=cast(0 as decimal(10,1))
into #t from sysobjects a,sysobjects b
set @i=@sectnum-@@rowcount
while @i>0
begin
set rowcount @i
insert #t(a) select 0 from sysobjects a,sysobjects b
set @i=@i-@@rowcount
end
set rowcount 0declare @step decimal(10,1)
set @step=(@max-@min)*1.0/@sectnum
update #t set a=id*@step,b=case id when @sectnum-1 then @max else (id+1)*@step-.1 end--统计处理
select 分数段=cast(a.a as varchar)+'-'+cast(a.b as varchar)
,人数=isnull(count(b.成绩),0)
from #t a left join 成绩表 b
on b.成绩 between a.a and a.b
group by a.a,a.b
go--删除处理的临时表
drop table #t--删除测试数据
drop table 成绩表/*--测试结果分数段 人数
-------------------- --------
0.0-1.9 1
2.0-3.9 1
4.0-5.9 1
6.0-7.9 1
8.0-10.0 5(所影响的行数为 5 行)--*/
insert 临时表 select '0.0-1.9',count(*) from 成绩表 where 成绩 between 0 and 1.9
union all select '2.0-3.9',count(*) from 成绩表 where 成绩 between 2 and 3.9
union all select '4.0-5.9',count(*) from 成绩表 where 成绩 between 4 and 5.9
union all select '6.0-7.9',count(*) from 成绩表 where 成绩 between 6 and 7.9
union all select '8.0-10',count(*) from 成绩表 where 成绩 between 8 and 10
select * from 临时表
create table 成绩表(姓名 varchar(10),成绩 decimal(10,1))
insert 成绩表 select 'aa',10
union all select 'bb',9
union all select 'cc',9
union all select 'dd',8
union all select 'ee',5
union all select 'ff',6
union all select 'gb',2
union all select 'hh',8
union all select 'ii',1
go--查询的存储过程
create proc p_qry
@min int=0 , --开始值
@max int=10, --结束值
@sectnum int=5 --组数,一共分多少组
as
--创建处理的临时表
declare @i int
set rowcount @sectnum
select id=identity(int,0,1),a=cast(0 as decimal(10,1)),b=cast(0 as decimal(10,1))
into #t from sysobjects a,sysobjects b
set @i=@sectnum-@@rowcount
while @i>0
begin
set rowcount @i
insert #t(a) select 0 from sysobjects a,sysobjects b
set @i=@i-@@rowcount
end
set rowcount 0declare @step decimal(10,1)
set @step=(@max-@min)*1.0/@sectnum
update #t set a=id*@step,b=case id when @sectnum-1 then @max else (id+1)*@step-.1 end--统计处理
select 分数段=cast(a.a as varchar)+'-'+cast(a.b as varchar)
,人数=isnull(count(b.成绩),0)
from #t a left join 成绩表 b
on b.成绩 between a.a and a.b
group by a.a,a.b
go--调用***********************只需要在调用的时候,改变调用参数即可
exec p_qry 0,10,5
exec p_qry 0,20,10
go--删除测试数据
drop table 成绩表
drop proc p_qry/*--测试结果--第一组测试:min=0,max=10,组数=5分数段 人数
--------------- ----------
0.0-1.9 1
2.0-3.9 1
4.0-5.9 1
6.0-7.9 1
8.0-10.0 5(所影响的行数为 5 行)
--第二组测试:min=0,max=20,组数=10分数段 人数
--------------- ----------
0.0-1.9 1
2.0-3.9 1
4.0-5.9 1
6.0-7.9 1
8.0-9.9 4
10.0-11.9 1
12.0-13.9 0
14.0-15.9 0
16.0-17.9 0
18.0-20.0 0(所影响的行数为 10 行)
--*/