【发布时间】:2022-01-18 19:48:27
【问题描述】:
我正在尝试为我的项目创建一种库存管理系统,并且非常感谢任何人提供的任何意见。
我正在尝试用我的Count 和Used 表的值差异来更新一个表(Current)。
这是我的表格的简化版。
Count
| date | donor | type_id | count |
|---|---|---|---|
| 03-JAN-20 | Bob | Rice | 20 |
| 05-JAN-20 | Jan | Pasta | 12 |
| 08-JAN-20 | Bob | Rice | 8 |
Used
| date | type_id | used |
|---|---|---|
| 03-JAN-20 | Rice | 13 |
| 05-JAN-20 | Pasta | 5 |
我想用这个公式更新我的当前表。
select (a.ct - b.us) as diff
from
(select sum(count) as ct, type_id from NATHANIA.inventory_fact where type_id = 'Rice' group by type_id) a
left join
(
select sum(used) as us, type_id from NATHANIA.inventory_used where type_id = 'Rice' group by type_id) b
on a.type_id = b.type_id;
输出给我 15 (20+8-13)。我无法相应地更新我的 Current 表。
Current
| type_id | current_count |
|---|---|
| Rice | 15 |
| Pasta | 2 |
我尝试使用 cte,但它给了我这个错误:“UPDATE ... SET 表达式必须是子查询”。
update NATHANIA.inventory_current t1
set (t1.type, t1.current_count) = (
with cte1 as (
select a.ct-b.us as diff
from
(select sum(count) as ct, type_id from NATHANIA.inventory_fact where type_id = 'Rice' group by type_id) a
left join
(select sum(used) as us, type_id from NATHANIA.inventory_used where type_id = 'Rice' group by type_id) b
on a.type_id = b.type_id
)
select type_id, diff from cte1 where t1.type = cte1.type_id
);
如果我遗漏任何信息,请告诉我,这是我的第一个堆栈问题 :)
【问题讨论】:
标签: sql oracle sql-update oracle-sqldeveloper