Solved

T-SQL Update Column from other Tbl

Posted on 2009-05-19
5
286 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

747 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