Solved

T-SQL Update Column from other Tbl

Posted on 2009-05-19
5
292 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
[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
  • 2
  • 2
5 Comments
 
LVL 143

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 143

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

737 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