案例來源:PTT DATABASE版
一個資料表(timess)如下
Date Value "Results"
05/01/01 300 300
05/01/02 200 500
05/01/03 -100 400
05/01/04 -200 200
05/01/05 500 700
. . .
. . .
. . .
我要如何取results值 永遠是自己日期以前的value相加
方法:
先建立pk(id),透過ID來UPDATE
*******************************
create or replace procedure pro01
is
i number;
j number;
begin
select max(id) into i from timess;
select min(id) into j from timess;
LOOP
exit when i<j;
update timess set Results=(select sum(value) from
timess where id<i) where id=i;
i:=i-1;
END LOOP;
end;
******************************
每次更新VALUE後 執行 execute pro01;
就可以維護這個資料表了
文章標籤
全站熱搜
