select date,sum(qty) as daysum,sum=(select sum(qty) from tablename where [date]<=a.[date]) from tablename group by [date]

解决方案 »

  1.   

    上面的错了:
    select [date],daysum,[sum]=(select sum(qty) from tablename where [date]<=a.[date]) from (select [date],sum(qty) as daysum from tablename  group by [date]) a
      

  2.   


    declare  @table table(id int identity(1,1) ,date varchar(10),qty int)
    insert into @table
    select  '20040101', 10
    union all
    select 
    '20040101', 20
    union all
    select 
    '20040102' ,10
    union all
    select 
    '20040103' ,20
    union all
    select 
    '20040104' ,30
    union all
    select 
    '20040105' ,10
    union all
    select 
    '20040105' ,10
    union all
    select 
    '20040106' ,50
    union all
    select 
    '20040106' ,10
    union all
    select 
    '20040106' ,10
    select date,sum(qty),(select sum(qty) from @table where date<=O.date)
    from @table O
    group by date结果
    20040101 30 30
    20040102 10 40
    20040103 20 60
    20040104 30 90
    20040105 20 110
    20040106 70 180
      

  3.   

    Select DATE, Sum(QTY) As daysum, sum = (Select sum(qty) From 表 
    Where date <= a.[date]))  From 表 Order by date Group by date
      

  4.   

    Select DATE, Sum(QTY) As daysum, sum = (Select sum(qty) From 表 
    Where date <= a.[date]))  From 表 as a Order by date Group by date