Solved

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

Posted on 2013-01-08
3
209 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks to both comments
acperkins, this worked perfect and was simple. Cheers
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now