本帖最后由 gzyes 于 2010-03-12 12:18:44 编辑

解决方案 »

  1.   

    if not object_id('tb') is null
    drop table tb
    Go
    Create table tb([Date] decimal(18,1),[Qty] int)
    Insert tb
    select 1.1,50 union all
    select 1.2,100 union all
    select 1.3,70 union all
    select 1.4,1
    Go
    select *,
           [all]=(select sum([Qty])from tb where [date]<=t.[date])
    from tb t
    /*
    Date                                    Qty         all
    --------------------------------------- ----------- -----------
    1.1                                     50          50
    1.2                                     100         150
    1.3                                     70          220
    1.4                                     1           221
    */
      

  2.   

    select *,[Total]=(select sum([Qty])
                       from tb where [date]< = T.[date])
    from tb T
      

  3.   

    Create table tb(Date decimal(18,1),Qty int)
    Insert tb
    select 1.1,50 union all
    select 1.2,100 union all
    select 1.3,70 union all
    select 1.4,1select * , Total = (select sum(Qty)from tb where date <= t.date) from tb tdrop table tb/*
    Date                 Qty         Total       
    -------------------- ----------- ----------- 
    1.1                  50          50
    1.2                  100         150
    1.3                  70          220
    1.4                  1           221(所影响的行数为 4 行)
    */
      

  4.   

    if not object_id('tb') is null
        drop table tb
    Go
    Create table tb([Date] decimal(18,1),[Qty] int)
    Insert tb
    select 1.1,50 union all
    select 1.2,100 union all
    select 1.3,70 union all
    select 1.4,1
    Go
    select *,
           (select sum([Qty])from tb where [date]<=t.[date]) 'total'
    from tb tDate                                    Qty         total
    --------------------------------------- ----------- -----------
    1.1                                     50          50
    1.2                                     100         150
    1.3                                     70          220
    1.4                                     1           221(4 行受影响)