Link to home
Start Free TrialLog in
Avatar of louise_8
louise_8

asked on

updating column in table 1 by calculating using columns in table 2

Hi,

How can I do the following please?

I want to update a total column in 1 table using 2 columns in another table for multiple rows.

This is the rough query, except I get the error
'Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.'

eg

update tbPay
set total = (
SELECT      SUM(mPrice * iQty - ( (mPrice * iQty) * CONVERT(float, vCustom1)/100)) as CostIncDiscount
FROM         dbo.tbItem INNER JOIN
                      dbo.tbPurchase INNER JOIN
                      dbo.tbWholesalerP ON dbo.tbPurchase.iPurchaseId = dbo.tbWholesalerP.iPurchaseId INNER JOIN
                      dbo.tbPay ON dbo.tbPurchase.iPurchaseId = dbo.tbPay.iPurchaseId ON dbo.dbo.tbItem.iPurchaseId = dbo.tbPurchase.iPurchaseId
WHERE  (dbo.dbo.tbItem.iProductId BETWEEN 1 AND 4) AND
                      (dbo.dbo.tbItem.dtCancel IS NULL) AND   dbo.tbWholesalerP.dtDispatch > '01 dec 2012'
GROUP BY dbo.dbo.tbItem.iPurchaseId)
FROM         dbo.tbItem INNER JOIN
                      dbo.tbPurchase INNER JOIN
                      dbo.tbWholesalerP ON dbo.tbPurchase.iPurchaseId = dbo.tbWholesalerP.iPurchaseId INNER JOIN
                      dbo.tbPay ON dbo.tbPurchase.iPurchaseId = dbo.tbPay.iPurchaseId ON dbo.dbo.tbItem.iPurchaseId = dbo.tbPurchase.iPurchaseId
WHERE (dbo.dbo.tbItem.iProductId BETWEEN 1 AND 4) AND
                      (dbo.dbo.tbItem.dtCancel IS NULL) AND   dbo.tbWholesalerP.dtDispatch > '01 dec 2012'
Avatar of appari
appari
Flag of India image

try this

update tbPay
set total = A.CostIncDiscount
FROM         dbo.tbItem INNER JOIN
                      dbo.tbPurchase INNER JOIN
                      dbo.tbWholesalerP ON dbo.tbPurchase.iPurchaseId = dbo.tbWholesalerP.iPurchaseId INNER JOIN
                      dbo.tbPay ON dbo.tbPurchase.iPurchaseId = dbo.tbPay.iPurchaseId ON dbo.dbo.tbItem.iPurchaseId = dbo.tbPurchase.iPurchaseId
Inner Join (
SELECT      tbItem.iPurchaseId, SUM(mPrice * iQty - ( (mPrice * iQty) * CONVERT(float, vCustom1)/100)) as CostIncDiscount
FROM         dbo.tbItem INNER JOIN
                      dbo.tbPurchase INNER JOIN
                      dbo.tbWholesalerP ON dbo.tbPurchase.iPurchaseId = dbo.tbWholesalerP.iPurchaseId INNER JOIN
                      dbo.tbPay ON dbo.tbPurchase.iPurchaseId = dbo.tbPay.iPurchaseId ON dbo.dbo.tbItem.iPurchaseId = dbo.tbPurchase.iPurchaseId
WHERE  (dbo.dbo.tbItem.iProductId BETWEEN 1 AND 4) AND 
                      (dbo.dbo.tbItem.dtCancel IS NULL) AND   dbo.tbWholesalerP.dtDispatch > '01 dec 2012'
GROUP BY dbo.dbo.tbItem.iPurchaseId) A on A.iPurchaseId =  tbPay.iPurchaseId 
WHERE (dbo.dbo.tbItem.iProductId BETWEEN 1 AND 4) AND 
                      (dbo.dbo.tbItem.dtCancel IS NULL) AND   dbo.tbWholesalerP.dtDispatch > '01 dec 2012' 

Open in new window


Take backup of the existing data before executing the update statement.
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of louise_8
louise_8

ASKER

Thanks to both comments
acperkins, this worked perfect and was simple. Cheers