已知考试表(test),包含学生信息和成绩,现在想按分数段统计学生的考试情况,比如:从100分开始向下,每1分一档,统计成绩区间在[99,100],[98,99),[97.98)。这样的分数情况
我按照如下方法写,但每次只能统计最前面的分数段,并不能循环。
declare @a int
declare @b int
set @a=99
set @b=1
while @a>=90
begin
select * from score where Eng>= @a and Eng <@a+@b
set @a=@a-1
end
这样写,仅仅计算99-100分的情况。
请问问题出在什么地方?如何写才能实现我所说的功能?
谢谢大家

解决方案 »

  1.   

    declare @a int 
    declare @b int 
    set @a=99 
    set @b=1 
    while @a>=90  while @a>=0 
    begin 
    select * from score where Eng>= @a and Eng <@a+@b 
    set @a=@a-1 
    end 
      

  2.   


    declare @a int 
    declare @b int 
    set @a=99 
    set @b=1 
    while @a>=90 
    begin 
    select * from score where Eng>= @a and Eng <=@a+@b 
    set @a=@a-1 
    end 
      

  3.   

    where Eng>= @a and Eng <@a+@b 这样有满足条件的? 我怀疑  
      

  4.   


    select 
    [99-100]=sum(case when score between 99 and 100 then 1 else 0 end), 
    [98-99]=sum(case when score between 98 and 99 then 1 else 0 end), 
    [97-98]=sum(case when score between 97 and 98 then 1 else 0 end), 
    [96-97]=sum(case when score between 96 and 97 then 1 else 0 end), 
    ....................................
    from score
      

  5.   

    已知考试表(test),包含学生信息和成绩,现在想按分数段统计学生的考试情况?
    是什么的情况。是人数呢?还是什么。你要的结果是怎么样的。请楼主说明清楚。
      

  6.   

    我还以为是统计分数,那用union all
      

  7.   


    declare @a int 
    declare @b int 
    set @a=99 
    set @b=1 
    while @a>=90 
    begin 
    select * from score where Eng>= @a and Eng <@a+@b 
    set @a=@a-1 
    set @b=@b+1
    end 
      

  8.   

    declare @a int 
    declare @b int 
    set @a=99 
    set @b=1 
    while @a>=90 
    begin 
    select * from score where Eng>= @a and Eng <@a+@b 
    set @a=@a-1 
    set @b=@b+1
    end 
      

  9.   


    declare @a int 
    declare @sql varchar(3000)
    set @a=99 
    while @a>=90 
    begin 
    set @sql=@sql+ 'union all select * from score where Eng between '+@a+' and '+(@a+1)+' '
    set @a=@a-1 
    end 
    set @sql=right(@sql,len(@sql)-10)
    exec(@sql)
      

  10.   

    declare @a int 
    declare @b int 
    set @a=99 
    set @b=1 
    while @a>=90 
    begin
    set @b=@b+1  
    select * from score where Eng>= @a and Eng <@a+@b 
    set @a=@a-1 
    end 
      

  11.   

    楼主你说每一次只能返回一个结果,你应该不是在sql 查询分析器中 执行的,你是在客户端程序执行的结果。对于服务器返回多个结果集的 sql语句,客户端程序一般只处理第一个结果。但是很多是可以处理多结果集的。楼主要去查一下,我了解的ADO就可以。
      

  12.   

    在sql中可以使用for循环吗,怎么使用?就是使用格式,例如:我要向表里面插入1000条数据要怎么做才能实现
      

  13.   

    象LZ的情况,
    建议就一select 语句将记录全部取出,在客户端使用recordset的filter属性过滤出所需数据。按LZ的写法,一次返回10个结果集,客户端需用10个recordset来放数据
    如果是用ado返回结果集,执行后返回第一个结果集(recordset)
    recordset.nextrecordset方法可返回第二个recordset,不断执行nextrecordset
    可返回LZ需要的全部10个recordset
      

  14.   

    简单的情况可在客户端用一for循环执行10次命令取出10个结果集,比一次取出会更稳定.