select * from table
查出结果是:
TheMoney
----------------------
8900
8800
-6000
-8000
-9000
-8789
我要的结果是可以上个加下个的值,就是让它自动计算,又要有6个值,请问怎么做?结果:TheMoney
----------------------
8900
8900+8800
8900+8800-6000
8900+8800-6000-8000
8900+8800-6000-8000-9000
8900+8800-6000-8000-9000-8789
sql语句怎么写?高手们啊,救命啊

解决方案 »

  1.   

    select tid=identity(int,1,1),* into # from [table]select 
      (select sum(TheMoney) from # where id<=t.id) as TheMoney
    from # tdrop table #
      

  2.   

    ---测试数据---
    if object_id('[table]') is not null drop table [table]
    go
    create table [table]([TheMoney] int)
    insert [table]
    select 8900 union all
    select 8800 union all
    select -6000 union all
    select -8000 union all
    select -9000 union all
    select -8789
     
    ---查询---
    select tid=identity(int,1,1),* into # from [table]select 
      (select sum(TheMoney) from # where tid<=t.tid) as TheMoney
    from # tdrop table #/**
    TheMoney    
    ----------- 
    8900
    17700
    11700
    3700
    -5300
    -14089(所影响的行数为 6 行)
    **/