大家好,我想请问一个表,如何根据记录数的不同,如何分别分次累计不同记录数的不同总和。如
A      B           C
我     8-1        3
我     8-2         7
你     8-3        10
我     8-4       5
你     8-4       9要获得的结果A      B           C
我     8-1        3
我     8-2         10(=3+7)
你     8-3        10
我     8-4       15(=3+7+5)
你     8-4       19(=10+9)

解决方案 »

  1.   

    select a,b,c=(select sum(c) from 表名 where a=a.a and b<=a.b) from 表名 a
      

  2.   

    回楼上,尝试了不行,你返回的c字段是所有日期的累计,不是不同日期的不同累计这个问题可以这么理解,select a,max(b),sum(c) from 表名 group by a having b<=
    (取遍b的所有值)
      

  3.   


    if object_id('tb') is not null
    drop table tb
    go
    create table tb(a varchar(2),b varchar(5),c int)
    insert into tb select '我',    '8-1',        3 
    union all select '我',    '8-2',    7 
    union all select '你',    '8-3',     10 
    union all select '我',    '8-4',    5 
    union all select '你',    '8-4',    9 
    --假设b列-后面只有一位数字
    select a,b,
    c=
    (
    select sum(c) from tb 
     where a=b.a 
    and abs(right(b,1))<=abs(right(b.b,1))

    from tb b drop table tb
    /*
    (5 行受影响)
    a    b     c
    ---- ----- -----------
    我    8-1   3
    我    8-2   10
    你    8-3   10
    我    8-4   15
    你    8-4   19(5 行受影响)*/
      

  4.   


    if object_id('tb') is not null 
    drop table tb 
    go 
    create table tb(a varchar(2),b varchar(5),c int) 
    insert into tb select '我',    '8-1',        3 
    union all select '我',    '8-2',    7 
    union all select '你',    '8-3',    10 
    union all select '我',    '8-4',    5 
    union all select '你',    '8-4',    9 
    --假设b列-后面只有一位数字 
    select a,b, 
    c= 

    select sum(c) from tb 
    where a=b.a 
    and abs(right(b,1)) <=abs(right(b.b,1)) 

    from tb b drop table tb 
    /* 
    (5 行受影响) 
    a    b    c 
    ---- ----- ----------- 
    我    8-1  3 
    我    8-2  10 
    你    8-3  10 
    我    8-4  15 
    你    8-4  19 (5 行受影响) */
    --g给点分吧,不要再发0分贴了
      

  5.   

    真是有眼不识泰山,pt1314917 Beirut两位老兄的都对了,厉害,感谢!由于没金币了,我对二位的精神和技术表示崇高的敬意!