表格如下
id a b c
1 12 19 0
2 15 17 0
5 11 32 0
6 18 79 0
3 23 342 0
4 134 545 0结果:
id a b c
1 12 19 0
2 15 17 228
5 11 32 81379
6 18 79 81731
3 23 342 483
4 134 545 8349
说明
ID=2的C列值为ID=1行的A*B+C
ID=3的C列值为ID=2行的A*B+C
ID=6的C列值为ID=5行的A*B+C
依次至最后一行------------
以你的计算方式:
c4 = a3*b3+a2*b2+a1*b1;
如果写SQL语句会很复杂,不如用一个函数解决。
函数如下:create or replace function haha(id number) return number
is 
rs number;
a number;
b number;
begin
rs:=0;
for i in 1..id-1 loop
  select a,b into a,b from test te where te.id = i;
  rs :=rs+a*b;
end loop;
return rs;
end haha;
最后的查询语句为:
select te.id,te.a,te.b ,haha(te.id) c from test;

解决方案 »

  1.   

    原帖在这里,http://topic.csdn.net/u/20101228/19/35fc4c99-e649-4553-b332-4fb93d80ecef.html?98586
    其实分析函数就解决了的,当然函数也可以的
      

  2.   


    --自定义函数可以简单点
    create or replace function haha(id number) 
    return number
    is 
    rs  number:=0;
    begin 
         select sum(a*b) into rs from test where id < i;
         return  rs;
    end haha;
      

  3.   


    --
    create or replace function haha(i number)  --这里是参数i
    return number
    is 
      rs  number:=0;
    begin 
         begin
              select sum(a*b) into rs from test where id < i;
         exception when others then
              raise_application_error(-20120,'** haha() error **');
         end;
         return rs;
    end haha;