简体   繁体   English

SQL-Oracle:使用来自另一个表的值的总和更新表列-使用公共​​键列条件

[英]SQL-Oracle: Updating table column with sum of values from another table - using common key column/s criteria

Good Evening Stackoverflow team/members 晚上好,Stackoverflow团队/成员

Oracle version: 11g Release 11.1.0.6.0 - 64bit Production Oracle版本:11g 11.1.0.6.0版-64位生产

I have two Tables: ORDERS and ITEMS. 我有两个表:ORDERS和ITEMS。

ORDERS looks like this 订单看起来像这样

ORDERS 命令

ITEMS looks like this 项目看起来像这样

enter image description here 在此处输入图片说明

Table ORDERS has 1 or more Order_Number each assigned to at least one depot or more. 表ORDERS具有1个或多个Order_Number,每个Order_Number被分配给至少一个或多个仓库。 the column Total_Value SHOULD store exactly the sum of the item values associated to an order number. Total_Value列应准确存储与订单号关联的项目值的总和。 the table ITEMS in fact stores via parcel_code items values for a specific order_number/s. 实际上,ITEMS表通过parcel_code项目存储了特定order_number / s的值。

my db has a bug for orders that have more than one depot assigned to an order number (eg order number 1 and 4) do not store the actual total value correctly. 我的数据库有一个错误,该错误是将多个仓库分配给一个订单编号(例如,订单编号1和4)的订单未正确存储实际总值。

in my case I cannot figure out the UPDATE statement that would select order numbers that would take the sum from the table ITEMS and link it via parcel_code and update the column total_value in the table ORDERS. 在我的情况下,我无法弄清将选择订单号的UPDATE语句,该订单号将从表ITEMS中获取总和,并通过parcel_code进行链接,并更新表ORDERS中的total_value列。

the result of my update should give this back: 我更新的结果应该给这个:

for table orders i should get back for 餐桌订单我应该回来

order number 1: for both rows total value 1120 订单号1:两行总计1120

order number 4 for both rows total value: 350 两行的订单号4总价值:350

order number 2 and 3 as they are single depot order remain the same: 50 and 20 订单号2和3(因为它们是单个仓库订单)保持不变:50和20

pseudo-code : 伪代码:

update ORDERS set total_value = (select sum(I.item_value) from ITEMS I, ORDERS O where O.parcel_code = I.parcel_code) 更新ORDERS设置total_value =(从ITEMS I,ORDERS O中选择sum(I.item_value),其中O.parcel_code = I.parcel_code)

i would update also those orders which have on e depot assigned as they will exactly the same. 我还将更新在电子仓库上分配的订单,因为它们将完全相同。

i was looking at MERGE statements or INNER select queries. 我在看MERGE语句或INNER选择查询。 the problem i am facing is that my update must be dynamic. 我面临的问题是我的更新必须是动态的。 this means not driven by values but by columns joins as i may have to create a process that update this every day. 这意味着不是由值驱动而是由列连接驱动,因为我可能必须创建一个每天更新的过程。

can someone help me? 有人能帮我吗?

You need to join the items and orders tables together, then select the sum of all item_values where your order_number is equal to the row which you are updating (in table o1). 您需要将items和orders表连接在一起,然后选择order_number等于要更新的​​行(在表o1中)的所有item_values的总和。

update 
Orders o1
set o1.total_value = (
    select sum(i.item_value) from Items i
    join Orders o2 on o2.parcel_code = i.parcel_code
    where o2.Order_number = o1.Order_number
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Oracle SQL:使用另一个表的 SUM 查询更新列 - Oracle SQL : Updating a column with SUM query of another table 使用LAG()从自身进行SQL-Oracle更新表 - SQL-Oracle Update table from itself using LAG() 使用递归的SQL-Oracle Update表 - SQL-Oracle Update table using recursivity SQL-使用另一个表的列作为条件搜索表 - SQL - Searching a table using another table's column as criteria 通过使用另一个公共列作为 sql 中 Atlassian confluence 表转换器宏中的键来添加 2 个表的列的值 - Adding values of column of 2 tables by using another common column as key in sql in table transformer macro in Atlassian confluence SQL-Oracle:根据同一表中包含的值更新表多行 - SQL-Oracle: Updating table multiple row based on values contained in the same table 使用另一个表中的列值更新一个表中的列的值SQL? - Updating the values of a column in one table with the values of a column in another table SQL? 使用SQL中另一个表的列更新表 - updating a table with a column from another table in SQL 使用 SQL 中另一个表的左连接更新表中的列 - Updating column in table using left join from another table in SQL 将一个sql列基于两个列求和,将另一个表与条件求和 - sum one sql column based on two columns another table with criteria
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM