Solved

T-SQL Update Column from other Tbl

Posted on 2009-05-19
5
288 Views
Last Modified: 2012-05-07
I'm trying to update a column of one tbl with the sum of several columns of another joined tbl and receive the following error:

Msg 157, Level 15, State 1, Line 2
An aggregate may not appear in the set list of an UPDATE statement.

How would I accomplish this? Thanks!!
update e

	set Paid = (Sum(a.LossPaid-a.LossRecoveredSub-a.LossRecoveredSal-a.LossRecoveredOther)) from p

					where e..ClaimNumber = p.ClaimNumber)

	from SAP2007ClaimMaster e

		where exists (select null from p

						where e.ClaimNumber = p.ClaimNumber)

Open in new window

0
Comment
Question by:Rickzzz
  • 2
  • 2
5 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24426921
this should do:
update e
  set Paid = (select Sum(p.LossPaid-p.LossRecoveredSub-p.LossRecoveredSal-p.LossRecoveredOther)
                from p
               where e.ClaimNumber = p.ClaimNumber
             )
  from SAP2007ClaimMaster e
  where exists (select null from p
                  where e.ClaimNumber = p.ClaimNumber
                   )

Open in new window

0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24426936
i dont know what 'a.' stands for

you can modify the query in this way

Update E
SEt Paid = a.Paid
FROM  SAP2007ClaimMaster e
INNER JOIN (
 SELECT ClaimNumber, Sum(a.LossPaid-a.LossRecoveredSub-a.LossRecoveredSal-a.LossRecoveredOther)
 FROM p
 )P  on e.ClaimNumber = p.ClaimNumber
0
 

Author Comment

by:Rickzzz
ID: 24426990
Not 'a.', should have been 'p.'...

There is something not working with your code, or how I'm using it anyway. Is that suppose to what I intended above, w/o the error? I may be goofing something.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24427009
can any of the values from "p" be null?

the suggestion with JOIN does not work, as you cannot SUM() in a UPDATE directly without subquery/inline view.
0
 

Author Closing Comment

by:Rickzzz
ID: 31583258
Perfect... thanks angelIII
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
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.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

27 Experts available now in Live!

Get 1:1 Help Now