有表
单据号   单据日期          数量
-------------------------------------
001      2010-12-01    2
002      2010-12-02    3
003      2010-12-03    4
004      2010-12-01    5我如何得到如下的表:
单据号   单据日期      目前已完成的数量
-------------------------------------------
001      2010-12-01      7  ( 小于等于本条的单据日期的数量汇总: 2+5)
001      2010-12-02      10 (小于等于本条的单据日期的数量汇总: 2+3+5)
001      2010-12-03      14 (小于等于本条的单据日期的数量汇总: 2+3+4+5)
001      2010-12-01      7  (小于等于本条的单据日期的数量汇总: 2+5)
盼望速回,先谢谢大家!

解决方案 »

  1.   

    create table tb
    (
        单据号 varchar(3),
        单据日期 datetime,
        数量 int
    )
    insert into tb
    select '001','2010-12-01',2 union all
    select '002','2010-12-02',3 union all
    select '003','2010-12-03',4 union all
    select '004','2010-12-01',5select 单据号
    ,单据日期
    ,(select sum(数量) from tb b where A.单据日期>=b.单据日期) as 数量
    from tb a
    -----------------------
    单据号 单据日期 数量
    001 2010-12-01 00:00:00.000 7
    002 2010-12-02 00:00:00.000 10
    003 2010-12-03 00:00:00.000 14
    004 2010-12-01 00:00:00.000 7
      

  2.   

    select 单据号, 单据日期, 
    目前已完成的数量=(select sum(数量) from tb where 单据日期<=t.单据日期)
    from tb t
      

  3.   

    create table tb (单据号 varchar(10),单据日期 datetime,数量 int)
    insert tb select '001', '2010-12-01', 2 union all 
    select '002','2010-12-02',3 union all 
    select '003','2010-12-03',4 union all 
    select '004','2010-12-01',5select '001' 单据号,convert(varchar(10),单据日期,121)  单据日期,
    目前已完成的数量=(select sum(数量) from tb 
    where  单据日期<=a.单据日期) from tb  a /*
    001 2010-12-01 7
    001 2010-12-02 10
    001 2010-12-03 14
    001 2010-12-01 7
    */