Solved

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

Posted on 2013-01-08
3
214 Views
Last Modified: 2013-01-08
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'
0
Comment
Question by:louise_8
3 Comments
 
LVL 39

Expert Comment

by:appari
ID: 38757353
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.
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 150 total points
ID: 38757370
Something like this perhaps:
UPDATE  tbPay
SET     total = x.CostIncDiscount
FROM    dbo.tbPay y
        INNER JOIN (SELECT  i.iPurchaseId,
                            SUM(mPrice * iQty - ((mPrice * iQty) * CONVERT(float, vCustom1) / 100)) AS CostIncDiscount
                    FROM    dbo.tbItem i
                            INNER JOIN dbo.tbPurchase p ON i.iPurchaseId = p.iPurchaseId
                            INNER JOIN dbo.tbWholesalerP w ON i.iPurchaseId = w.iPurchaseId
                    WHERE   i.iProductId BETWEEN 1 AND 4
                            AND i.dtCancel IS NULL
                            AND w.dtDispatch > '01 dec 2012'
                    GROUP BY i.iPurchaseId
                   ) x ON y.iPurchaseId = x.iPurchaseId 

Open in new window

0
 

Author Closing Comment

by:louise_8
ID: 38757484
Thanks to both comments
acperkins, this worked perfect and was simple. Cheers
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Addition to SQL for dynamic fields 6 38
VBScript Write Column Headers 3 37
SQL Improvement  ( Speed) 14 27
SQL Query assistance 16 23
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question