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.iPurchas eId 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.dtDispat ch > '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.iPurchas eId 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.dtDispat ch > '01 dec 2012'
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.tbPay ON dbo.tbPurchase.iPurchaseId
WHERE (dbo.dbo.tbItem.iProductId
(dbo.dbo.tbItem.dtCancel IS NULL) AND dbo.tbWholesalerP.dtDispat
GROUP BY dbo.dbo.tbItem.iPurchaseId
FROM dbo.tbItem INNER JOIN
dbo.tbPurchase INNER JOIN
dbo.tbWholesalerP ON dbo.tbPurchase.iPurchaseId
dbo.tbPay ON dbo.tbPurchase.iPurchaseId
WHERE (dbo.dbo.tbItem.iProductId
(dbo.dbo.tbItem.dtCancel IS NULL) AND dbo.tbWholesalerP.dtDispat
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks to both comments
acperkins, this worked perfect and was simple. Cheers
acperkins, this worked perfect and was simple. Cheers
Open in new window
Take backup of the existing data before executing the update statement.