表1
===============================
BEGIN   IN  OUT END DATE
100     10  20      2008-07-01  
        20  30      2008-07-02
        50  20      2008-07-03表2
===============================                
BEGIN   IN  OUT END DATE
100     10  20  90  2008-07-01  
90      20  30  80  2008-07-02
80      50  20  110 2008-07-03END = BEGIN + IN - OUT
BEGIN = 上一天END请问如何用一条SQL将表1更新到表2?

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【CHARGING】截止到2008-07-25 17:03:26的历史汇总数据(不包括此帖):
    发帖的总数量:5                        发帖的总分数:520                      每贴平均分数:104                      
    回帖的总数量:7                        得分贴总数量:2                        回帖的得分率:28%                      
    结贴的总数量:5                        结贴的总分数:520                      
    无满意结贴数:0                        无满意结贴分:0                        
    未结的帖子数:0                        未结的总分数:0                        
    结贴的百分比:100.00%               结分的百分比:100.00%                  
    无满意结贴率:0.00  %               无满意结分率:0.00  %                  
    敬礼!
      

  2.   

    update table1 set BEGIN = (nvl(BEGIN,select END from table1 where DATE = trunc(DATE)-1)), END = (nvl(BEGIN,select END from table1 where DATE = trunc(DATE)-1)+IN-OUT);不知道是否可以运行 手边没有oracle环境. 大概思路就是这个了,子查询应该可以优化.
      

  3.   

    楼上的, 貌似不行啊, 我的是MySQL, 换成对应的函数也执行不了.
    update test a set a.e_begin = ifnull(a.e_begin, (select b.e_end from test b where b.demand_date=date_add(a.demand_date, interval -1 day))),
    a.e_end=ifnull(a.e_end, ((select c.e_end from test c where c.demand_date = date_add(a.demand_date, interval -1 day)) + a.e_in - a.e_out));Error Code : 1093
    You can't specify target table 'a' for update in FROM clause
    (0 ms taken)
      

  4.   

    本人在mysql5.0下调试过,可行。insert into test2
      (select (select max(begin) from test) +
              IFNULL((select sum(t.IN) - sum(t.OUT)
                       from test t
                      where t.DATE < tt.DATE),
                     0) begin,
              tt.in,
              tt.out,
              (select max(begin) from test) +
              (select sum(t.IN) - sum(t.OUT) from test t where t.DATE <= tt.DATE) end,
              tt.date
         from test tt)
      

  5.   

    考虑用procedure来实现
    mssql版本,用了cursor,破方法。-- 建表
    create table t1(
    b int,
    i int,
    o int,
    e int,
    d datetime default getdate()
    )-- 插入数据
    insert into t1 
    select 100,10,20,null,'2008-07-01' union all
    select null,20,30,null,'2008-07-02' union all
    select null,50,20,null,'2008-07-03' union all
    select null,50,20,null,'2008-07-04'  -- 存储过程
    create procedure prDoSomething
    as
    declare @b int,
    @i int,
    @o int,
    @e int,
    @d datetime
    declare cur cursor for
    select b,i,o,d from t1
    open cur
    fetch next from cur into @b,@i,@o,@d
    while(@@fetch_status = 0)
    begin
    update t1 set e = @b + @i - @o where d = @d
    update t1 set b = @b + @i - @o where d = (select min(d) from t1 where d > @d)
    fetch next from cur into @b,@i,@o,@d
    end
    close cur
    deallocate cur-- 执行procedure
    exec prDoSomething-- 查询
    select * from t1-- 清理 
    drop table t1
    drop procedure prDoSomething
    [code=BatchFile]
    b           i           o           e           d                     
    ----------- ----------- ----------- ----------- -----------------------
    100         10          20          90          2008-07-01 00:00:00.000
    90          20          30          80          2008-07-02 00:00:00.000
    80          50          20          110         2008-07-03 00:00:00.000
    110         50          20          140         2008-07-04 00:00:00.000(所影响的行数为 4 行)[/code]
      

  6.   

    lz可以去MSSQL版提问寻求更好的方法,传说那里是CSDN最为强大的地方,hoho~