问题描述:
两个表以上 a,b,c
a: 编号/结果1/结果2/......
    001  0      0  
    002  0      0b: 编号/结果1/其他/......
   001   1     .....
   001   2     .....
   002   5     .....
   006   4     .....
c: 编号/结果2/其他/......
   001   3     .....
   001   4     .....
   002   1     .....
   004   2     .....  按照a 表为基准,按编号汇总b 表,c表, 对结果求和sum(结果) 将 结果1,结果2的结果(sum(结果1),sum(结果2))对应 update 到 a 表,求 Update 的实现方法。

解决方案 »

  1.   

    你的意思是不是B表的两个001的值Sum为3,加上C表001的值为7,再将7+3的值10保存到A表中的结果1?如果是这样,可以以A表为基表进行循环,得出结果,再将结果写回到A表中。具体体现为(只写SQL语句):
    Var
    A1:String;
    B1.C1:Integer;
    Begin
    With 表A  do begin
       While not eof do begin
         A1:=  表A.FieldByName(‘编号’).AsString;
         Select sum(表B) where 编号=A1;
         B1:=0;
         B1:=Query1.FieldByName(‘Sum’).AsInteger;     Select sum(表C) where 编号=A1;
         C1:= =Query1.FieldByName(‘Sum’).AsInteger;
         
         再把B1+C1写回到A表
         Next;
    End;
    End;
    End;
    我的方法很笨,但管用。我也在找比较好一些的方法。砖出来了,玉呢?
        
      

  2.   

    我应经用 update 解决了, 谢谢大家update a set a.结果1= tb.bb from a,(select 编号,sum(结果1) as bb from b group by 编号) tb where a.编号= b.编号
      

  3.   

    這樣在oracle中可以首先
    select c.編號,sum1, sum2
    from (select 編號,sum(結果1) as sum1 from b groub by 編號) c,
         (select 編號,sum(結果2) as sum2 from c groub by 編號) d
    where c.編號=d.編號
    group by c.編號while not adoquery1.eof do
    begin
       adoquery1.close;
       adoquery.sql.clear;
       adoquery.sql.add('update a set 結果1=:sum1,結果2=:sum2 where 編號=:編號1');
       adoquery.paramters.parambyname('sum1').value:=adoquery1.fieldbyname('sum1').asinteger;
       adoquery.paramters.parambyname('sum2').value:=adoquery1.fieldbyname('sum2').asinteger;
       adoquery.paramters.parambyname('編號1').value:=adoquery1.fieldbyname('編號1').asinteger;
       adoquery.ExecSQL;
       adoquery.next;
       
    end;ok了