我认为这是可能的,但广泛的研究和无数小时的避风港都没有得到回报。
我是在仓库里销售产品。我知道什么时候我会收到货物,我有每日销售预测(数量)。我需要计算结束库存。它基本上是库存 - 预测的运行总和。
问题是,当我用完产品时,第二天我就不会有负库存,因为运行总额会提示。在我收到另一批货物之前,库存将为零。它可以在预测中多次降至零(远远超过下面的示例)。
Visual of the dataset (desired column in yellow)
SQL Fiddle for cracking the case
这是我的实际查询:
SELECT FORECAST_DATE, DAYS_OUT, INBOUND_INVENTORY, FORECAST,
ENDING_INVENTORY AS DESIRED_RESULT,
SUM(INBOUND_INVENTORY) OVER (PARTITION BY NULL ORDER BY DAYS_OUT) -
SUM(FORECAST) OVER (PARTITION BY NULL ORDER BY DAYS_OUT) AS ENDING_INVENTORY
FROM MRP
列
日期:预测日期(今天开始)
Days Out :今天和预测日期之间的天数
入库库存:产品进入(今天,产品在这里)
预测:我的预计销售数量
结束广告资源:入库广告资源 - 预测+如果昨天的结束广告资源<= 0,那么昨天结束广告资源就是0。
答案 0 :(得分:1)
这可能是正确的吗?它叫做递归公用表表达式。
WITH
cte_mrp as
(
Select row_number() over (partition by null order by forecast_date) as line, mrp.*
From mrp
),
RCTE (line, forecast_date, days_out, inbound_inventory, forecast, /*iteration, anchor,*/ ending_inventory) as
(
Select line, forecast_date, days_out, inbound_inventory, forecast, /*0 iteration, 'anchor' anchor,*/
CASE WHEN inbound_inventory-forecast < 0 THEN 0 ELSE inbound_inventory-forecast END ending_inventory
From cte_mrp
Where line = 1
union all
Select m.line, m.forecast_date, m.days_out, m.inbound_inventory, m.forecast, /*r.iteration + 1, 'rcte' anchor,*/
CASE WHEN r.ending_inventory+m.inbound_inventory - m.forecast < 0 THEN 0 ELSE r.ending_inventory+m.inbound_inventory - m.forecast END ending_inventory
From cte_mrp m
Inner join rcte r on (r.line = (m.line-1))
)
Select * From RCTE;
答案 1 :(得分:1)
这是使用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.