[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

T-SQL Update Column from other Tbl

Posted on 2009-05-19
5
Medium Priority
?
296 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

656 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