我有两张table。
表A**
| 产品|植物|股票|
| --|--|--|
| 11248 |ABC12| 65 |
| 98490 |GHI34| 54 |
| 11248 |DEF56| 25 |
表B
| 产品|植物|周|销售|生产|
| --|--|--|--|--|
| 11248 |ABC12| 0 | 7894 | 38410 |
| 11248 |ABC12|......这是什么?|......这是什么?|......这是什么?|
| 11248 |ABC12| 11 | 6887 | 84351 |
首先,我想添加表A中第0周的唯一股票价值,如下表所示
| 产品|植物|周|销售|生产|股票|
| --|--|--|--|--|--|
| 11248 |ABC12| 0 | 7894 | 38410 | 65 |
第二,使用操作(Production+Stock)-Sales
计算本周剩余时间的股票,并得到如下表。
| 产品|植物|周|销售|生产|股票|
| --|--|--|--|--|--|
| 11248 |ABC12| 0 | 7894 | 38410 | 65 |
| 11248 |ABC12| 1 | 6587 | 87427 |(87427+65)-6587= 80905|
| 11248 |ABC12|......这是什么?|......这是什么?|......这是什么?|......这是什么?|
| 11248 |ABC12| 11 | 6887 | 84351 |(84351+Stock_row10)-6887=NewStock|
我尝试两个查询
第一个查询:
with Table_A1 (Product, Plant, Stock) as (select Product, Plant, Stock from TABLE_A)
select Product,
Plant,
Week,
Sales,
Production,
from TABLE_B;
case
when Table_A1.Product = TABLE_B.Product and w.Plant=TABLE_B.Plant and TABLE_B.Week=0 then TABLE_A.Stock
when Table_A1.Product = TABLE_B.Product and w.Plant=TABLE_B.Plant and TABLE_B.Week=1 then (TABLE_B.Production+TABLE_A.Stock)-TABLE_B.Sales
end ;
但我得到了错误“错误:在“from”处或附近出现错误“
第二个查询:
ALTER TABLE TABLE_B
ADD Stock INT;
SELECT * from TABLE_B
inner join TABLE_A ON TABLE_A.Product=TABLE_B.Product
case
when Table_A1.Product = TABLE_B.Product and w.Plant=TABLE_B.Plant and TABLE_B.Week=0 then TABLE_A.Stock
end;
但我得到了错误“错误:在'CASE'处或附近出现错误“。有什么需要帮忙的吗?
1条答案
按热度按时间mo49yndu1#
为此使用一个窗口函数。
您的用例是复杂的,因为您想要的结果显示初始库存是第一周结束时的库存。我以前见过这种情况,我将第一周的销售和生产结转,以调整它们从以后的库存计算中排除:
工作fiddle.