I have two Tables ("Shortages", "Orders"). Shortages contains each Material once, Orders is all the Material orders. I am trying to update Shortages field CurrentWeek with the sum of all orders of a material from Orders.
I am not an SQL guru and this is stumping me. I have googled things but I can't seem to get anything to work.
I can write a select query that gets me what I want with all the WHERE
clauses but I can't get the values into the other table.
Any help is greatly appreciated!
It sounds like you want something like this:
UPDATE Shortages
SET CurrentWeek = (SELECT SUM(quantity)
FROM Orders
WHERE Orders.Material = Shortages.Material
AND <which records in Orders count toward the current Shortages record>)
WHERE <which records in Shortages you want to update>
Obviously you'll need to modify it with the proper column names.
Get the SUM()
first and then perform an update join. Something like below (** It's a demo query but hope you understood the point)
update Shortages s
join (
select orderid, sum(order_qty) as total_qty
from orders
group by orderid ) xxx on s.orderid = xxx.orderid
set s.CurrentWeek = xxx.total_qty;
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.