表 A 结构如下示例
字段 aa   bb      rq
     43    0 7月1日
     34 7月2日
     45 7月3日
     34 7月4日
     65 7月5日
     65 7月6日
     65 7月7日
     40 7月8日
    ...     ...   ....
    ...     ...   .... 
怎么样算出7-2、7-3、7-4......的js这个字段
  其中7-2的BB等于7-1的BB加AA
  其中7-3的BB等于7-2的BB加AA
  其中7-4的BB等于7-3的BB加AA
   一直这样下去
  即今天的BB这个字段的数据是上一天的字段AA+BB的数据
 
 怎么用SQL语法修改这个表
 我想要update怎么写这个语句得到结果的表数据是
字段 aa   bb      rq
     43    0 7月1日
     34    43 7月2日
     45    77 7月3日
     34    122 7月4日
     65    156 7月5日
     65    221  7月6日
     65    286 7月7日
    ...     ...   ....
    ...     ...   ....

解决方案 »

  1.   

    update a
    set bb=(select sum(aa) from a where rq<a1.rq)
    from a a1
    where rq>'7月1日'
      

  2.   

    --测试declare @a table (
    aa int,
    bb int,
    rq datetime
    )
    insert @a select
    43,    0, '2007-7-1'
    union all select
    34,    0, '2007-7-2'
    union all select
    45,    0, '2007-7-3'
    union all select
    34,    0, '2007-7-4'
    union all select
    65,    0, '2007-7-5'
    union all select
    65,    0, '2007-7-6'
    union all select
    65,    0, '2007-7-7'
    union all select
    40,    0, '2007-7-8'
    update a1
    set bb=(select sum(aa) from @a where rq<a1.rq)
    from @a a1
    where rq>'2007-7-1'select * from @a--结果
    aa          bb          rq                                                     
    ----------- ----------- ------------------------------------------------------ 
    43          0           2007-07-01 00:00:00.000
    34          43          2007-07-02 00:00:00.000
    45          77          2007-07-03 00:00:00.000
    34          122         2007-07-04 00:00:00.000
    65          156         2007-07-05 00:00:00.000
    65          221         2007-07-06 00:00:00.000
    65          286         2007-07-07 00:00:00.000
    40          351         2007-07-08 00:00:00.000(所影响的行数为 8 行)
      

  3.   

    其实可以带第一条记录一起更新declare @a table (
    aa int,
    bb int,
    rq datetime
    )
    insert @a select
    43,    0, '2007-7-1'
    union all select
    34,    0, '2007-7-2'
    union all select
    45,    0, '2007-7-3'
    union all select
    34,    0, '2007-7-4'
    union all select
    65,    0, '2007-7-5'
    union all select
    65,    0, '2007-7-6'
    union all select
    65,    0, '2007-7-7'
    union all select
    40,    0, '2007-7-8'
    update a1
    set bb=isnull((select sum(aa) from @a where rq<a1.rq),0)
    from @a a1select * from @a--结果
    aa          bb          rq                                                     
    ----------- ----------- ------------------------------------------------------ 
    43          0           2007-07-01 00:00:00.000
    34          43          2007-07-02 00:00:00.000
    45          77          2007-07-03 00:00:00.000
    34          122         2007-07-04 00:00:00.000
    65          156         2007-07-05 00:00:00.000
    65          221         2007-07-06 00:00:00.000
    65          286         2007-07-07 00:00:00.000
    40          351         2007-07-08 00:00:00.000(所影响的行数为 8 行)