這是一個使用MODEL子句(在Oracle 10中引入)的解決方案。
我沒有包括forecast_date
列 - days_out
就夠了。
with
inputs (days_out, inbound_inventory, forecast) as (
select 0, 24, 0 from dual union all
select 1, 0, 124 from dual union all
select 2, 0, 154 from dual union all
select 3, 0, 316 from dual union all
select 4, 780, 119 from dual union all
select 5, 780, 148 from dual union all
select 6, 780, 123 from dual union all
select 7, 0, 168 from dual union all
select 8, 0, 323 from dual union all
select 9, 0, 184 from dual union all
select 10, 0, 331 from dual union all
select 11, 0, 149 from dual union all
select 12, 0, 431 from dual union all
select 13, 0, 153 from dual union all
select 14, 0, 183 from dual union all
select 15, 0, 169 from dual union all
select 16, 0, 169 from dual union all
select 17, 780, 331 from dual
)
select days_out, inbound_inventory, forecast, ending_inventory
from inputs
model
dimension by (days_out)
measures (inbound_inventory, forecast, 0 ending_inventory)
rules update
iterate(1000000) until (previous(ending_inventory[iteration_number + 1]) is null)
(
ending_inventory[iteration_number] =
greatest (0, inbound_inventory[cv()] - forecast[cv()]
+ nvl(ending_inventory[cv() - 1], 0)
)
)
;
輸出:
DAYS_OUT INBOUND_INVENTORY FORECAST ENDING_INVENTORY
---------- ----------------- ---------- ----------------
0 24 0 24
1 0 124 0
2 0 154 0
3 0 316 0
4 780 119 661
5 780 148 1293
6 780 123 1950
7 0 168 1782
8 0 323 1459
9 0 184 1275
10 0 331 944
11 0 149 795
12 0 431 364
13 0 153 211
14 0 183 28
15 0 169 0
16 0 169 0
17 780 331 449
18 rows selected.
請在實際問題的查詢和數據。 –
SELECT FORECAST_DATE, DAYS_OUT, INBOUND_INVENTORY, 預測, ENDING_INVENTORY如DESIRED_RESULT, SUM(INBOUND_INVENTORY)OVER(PARTITION BY NULL ORDER BY DAYS_OUT) - SUM(預測)OVER(PARTITION BY NULL ORDER BY DAYS_OUT)AS ENDING_INVENTORY FROM MRP –
http://sqlfiddle.com/#!4/72750/10 –