create table testTable(
code char(1),
yyyymm number(6),
month_qty number(3),
stock_qty number(3),
modify_flag number(1));
insert into testTable values('A',200701,999,150,0);
insert into testTable values('A',200702,180,350,1);
insert into testTable values('A',200703, 50,450,1);
insert into testTable values('A',200704, 50,500,0);
insert into testTable values('B',200701,999, 15,0);
insert into testTable values('B',200702, 20, 35,0);
insert into testTable values('B',200703, 8, 45,1);
insert into testTable values('B',200704, 5, 50,0);
commit;
SQL
--■■■分析関数を使う方法1■■■
update testTable a
set stock_qty = (select max(stock_qty) Keep (Dense_Rank First order by yyyymm)
- max(month_qty) Keep (Dense_Rank First order by yyyymm)
+ sum(month_qty)
from testTable b
where b.code = a.code
and b.yyyymm <= a.yyyymm),modify_flag = 0
where yyyymm >= (select min(b.yyyymm)
from testTable b
where b.code = a.code
and b.modify_flag = 1);
--■■■分析関数を使う方法2■■■
update testTable a
set stock_qty = (select distinct
First_Value(stock_qty) over(order by yyyymm)
- First_Value(month_qty) over(order by yyyymm)
+ sum(month_qty) over()
from testTable b
where b.code = a.code
and b.yyyymm <= a.yyyymm),modify_flag = 0
where yyyymm >= (select min(b.yyyymm)
from testTable b
where b.code = a.code
and b.modify_flag = 1);
解説
分析関数を使う方法2では、
First_Value関数とsum関数で、
最小のyyyymmの、stock_qtyを相殺してます