1.总成绩表
ClassID StudentID 语文 数学  总分
------------------------------------
000005 000001 150    130    280
000005 000003 140    90     230
000004 000006 130    100    230
000004 000003 60     90     150
2、分段间隔 N=10 可能N=20
结果:q1
         语文 数学
150-140    2   0
140-130    1   0 
130-120    0   0 .....
70-60      1   0
  (要 在access 中用呀)

解决方案 »

  1.   

    create table t
    (ClassID varchar(10), StudentID varchar(100), 語文 int, 數學 int,  總分 int)
    insert into t
    select '000005', '000001', 150  ,  130  ,  280 union all
    select '000005', '000003', 140  ,  90   ,  230 union all
    select '000004', '000006', 130  ,  100  ,  230 union all
    select '000004', '000003', 60   ,  90   ,  150select * from tdeclare @str varchar(12)
    declare @i int
    declare @j int
    set @i=150
    set @j=1
    while 150-@j*10>=60 
    begin
    set @str=cast(@i as varchar(10))+'-'+cast(150-@j*10 as varchar(10))
    select @str as 范圍,sum(case when 語文 between 150-@j*10 and @i then 1 else 0 end )as 語文,
                       sum(case when 數學 between 150-@j*10 and @i then 1 else 0 end )as 數學
    from t set @i=150-@j*10
    set @j=@j+1
    end
    范圍           語文          數學          
    ------------ ----------- ----------- 
    150-140      2           0(1 row(s) affected)范圍           語文          數學          
    ------------ ----------- ----------- 
    140-130      2           1(1 row(s) affected)范圍           語文          數學          
    ------------ ----------- ----------- 
    130-120      1           1(1 row(s) affected)范圍           語文          數學          
    ------------ ----------- ----------- 
    120-110      0           0(1 row(s) affected)范圍           語文          數學          
    ------------ ----------- ----------- 
    110-100      0           1(1 row(s) affected)范圍           語文          數學          
    ------------ ----------- ----------- 
    100-90       0           3(1 row(s) affected)范圍           語文          數學          
    ------------ ----------- ----------- 
    90-80        0           2(1 row(s) affected)范圍           語文          數學          
    ------------ ----------- ----------- 
    80-70        0           0(1 row(s) affected)范圍           語文          數學          
    ------------ ----------- ----------- 
    70-60        1           0(1 row(s) affected)