简体   繁体   English

Oracle SQL根据另一个表但根据日期更新列

[英]Oracle SQL Update a column based on another table but based on a date

Learning SQL so please forgive me. 学习SQL,请原谅我。

I have a table that holds many accounts(and sub accounts) and another table that holds many orders they are custom tables taken from different databases. 我有一个表,该表包含许多帐户(和子帐户),而另一个表则包含许多订单,它们是从不同数据库中获取的自定义表。

What I am trying to do is update order_amt on the account table with the order_val value from the orders table but if there are more than one order with that account I want the order amount from the earliest order date only. 我要执行的操作是使用订单表中的order_val值更新帐户表上的order_amt,但是如果该帐户有多个订单,我希望仅从最早的订单日期起订购金额。

Account Table 帐户表

Acc _Num..........Comp_Name.......Order_Amt.......Int_Id  
123456789-1.....ABC Ltd.......................................123456789  
123456789-2.....ABC Ltd.......................................123456789  
987654321-1.....Xyz Ltd.........................................987654321       
987654321-2.....Xyz Ltd.........................................987654321  

Orders Table 订单表

Order_Num.....Order_Dt.....Order_Val.....Acc_num  
1......................01/01/13......£20.00...........123456789    
2......................01/01/14......£10.00...........123456789  
3......................01/01/10......£100.00..........987654321  
4......................01/01/11......£200.00..........987654321  

So the order_amt for accounts 123456789-1 & 2 = £20.00 and from 987654321-1 & 2 would be £100.00. 因此,帐户123456789-1和2的order_amt =£20.00,而来自987654321-1&2的order_amt为£100.00。

UPDATE accounts a  
SET a.order_amt =  
(  
 SELECT order_val  
 FROM orders o  
 WHERE a.int_id = o.acc_num  
 AND EXISTS  
(  
 SELECT MIN(order_dt)  
 FROM orders oa  
 WHERE o.order_num = oa.order_num  
);

I am getting a few errors including the error that more than one row returned? 我收到一些错误,包括返回多行的错误? Could anyone please help me? 谁能帮我吗?

kind Regards 亲切的问候

Eden 伊甸园

I think you need something like this: 我认为您需要这样的东西:

UPDATE accounts a  
SET a.order_amt =  
(  
 SELECT order_val  
 FROM orders o  
 WHERE a.int_id = o.acc_num  
 GROUP BY order_val
 HAVING order_dt = MIN(order_dt);
);

Create a FK on the column account_number inside of the orders table and have it reference the auto-increment account id of the accounts table. 在订单表内的account_number列上创建FK ,并使其引用accounts表的auto-increment account id

Then as Roger suggest, with the slight modification of the column int_id AND a modification of the HAVING clause 然后,如Roger所建议的那样,对int_id列稍加修改,并对HAVING子句进行修改

UPDATE accounts a 
SET a.order_amount = 
(
    SELECT order_value
    FROM orders o 
    WHERE a.account_number = o.account_number

    GROUP BY order_value
    HAVING MIN(order_date)
)

This will not fix the error that is occurring with more than one row being returned though since there are multiple orders with the same account id attached to them. 尽管由于有多个订单附加了相同的帐户ID,但这不能解决返回多行的错误。 You will need to be more specific about how what you are needing. 您将需要更具体地了解您的需求。 Do you want the average of all order amounts to equal the order value in the accounts table? 您是否希望所有订单金额的平均值等于帐户表中的订单金额? Or maybe the MAX/MIN value? 还是最大/最小值?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM