简体   繁体   中英

Oracle SQL return sum of multiple across tables

Farily new to SQL and the IT world, and always do a lot of work before asking for help but this time I am stumped.

I have three tables:

在此处输入图片说明

So I am trying to update the "payment_owed" table by doing the following: For each customer, get the food_id and quantity, then using the food_id multiply the quantity by the cost.

The best I have done this far is a natural join on the tables, and attempting to sum the quantity * cost for each ID

My understanding for updating a specific customer:

update customer
set payment_owed = (select <quantity>) * (select <cost>)
where cust_no = 1;

If im not on the right forum or there's a better place to ask these questions let me know, thank you for your time!

Simply:

update customer
set payment_owed = (SELECT SUM(o.cost*s.quantity) 
                   FROM order o JOIN session s ON s.food_id = o.food_id         
                   WHERE s.cust_no = customer.cust_no)
where cust_no = 1;

Anyway will you update it after every change on table? How about using view like:

CREATE VIEW my_view AS
SELECT cust_no, SUM(o.cost*s.quantity) 
FROM order o 
JOIN session s ON s.food_id = o.food_id
GROUP BY cust_no;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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