Jump to content

Update rows with the sums of fields from other table


hiprakhar

Recommended Posts

Hi,

 

I have to update tempqty of all available products in the productlist. For this I have to calculate the difference of the Sums of each product purchased and sold.

 

I am writing this query for the same in MySql and MS access but they are not working. In Ms Access it says "must be an updateable query", in MySQL the sum of all the qty is update in the tempqty. This is the query.

 

UPDATE CopyProductBatch AS p SET p.tempqty =

(SELECT (a.total - b.total) FROM

(SELECT SUM(qty) AS total FROM Inventory AS i, CopyProductBatch AS p WHERE type = 'p' AND i.productbatchcode = p.productbatchcode) a,

(SELECT SUM(qty) AS total FROM Inventory AS i, CopyProductBatch AS p WHERE type = 's' AND i.productbatchcode = p.productbatchcode) b)

 

Please help

Link to comment
Share on other sites

For MySQL

 

UPDATE CopyProductBatch AS p 
LEFT JOIN (SELECT productbatchcode, SUM(qty) AS total FROM Inventory WHERE type = 'p' GROUP BY productbatchcode) AS a USING (productbatchcode)
LEFT JOIN (SELECT productbatchcode, SUM(qty) AS total FROM Inventory WHERE type = 's' GROUP BY productbatchcode) AS b USING (productbatchcode)
SET p.tempqty = IFNULL(a.total,0) - IFNULL(b.total,0)

Link to comment
Share on other sites

Thanks Mchl!!

 

The query works perfect in MySQL! One problem though in MS ACCESS it says "syntax error in update statement" and the cursor goes over "USING".

 

Is there any alias for this that works in MS ACCESS

 

Thanks for the support!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.