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 人数

解决方案 »

  1.   

    min=0,max=10这个是指定值吗?楼主的表结构又是怎么样的?
      

  2.   

    --处理示例--成绩表
    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 行)--*/
      

  3.   

    create table 临时表(分数段 varchar(10),数量 int)
    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 临时表
      

  4.   

    用case ...when ...语句做比较清晰,简洁不过要根据条件动态产生语句
      

  5.   

    谢谢!可都太具体了,我的min、max、sectnum都是由用户来设置的,怎么办呢?for...
      

  6.   

    楼主又是没有看吧?? 我写的max,min是自己定义的,不是写死的.我写成存储过程,你可能看得明白一点.
      

  7.   

    --处理示例--成绩表
    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 行)
    --*/