Solved

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

Posted on 2013-01-08
3
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

726 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