Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Update Table with Sum

Author  Topic 

batcater98
Starting Member

22 Posts

Posted - 2014-07-15 : 12:12:54
Trying to update a field in an table with a sum from itself. Can't quite get the syntax down.. Here is what I have.. What am I missing.

Update EventData
Set DivTotal = (Select EventKey, SUM(EventPoints) as DivTotal
From EventData
Where eventcode = 'F'
Group by Eventkey)

I know I probably need to be doing an inner join of something to make this crude statment work, but brain not working today.

Help Please.
Ad.

Regards,
The Dark Knight
-Give What is Right, Not What is Left-

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-07-15 : 12:46:12
Update EventData
Set DivTotal = (Select SUM(EventPoints) as DivTotal
From EventData
Where eventcode = 'F'
Group by Eventkey)

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

batcater98
Starting Member

22 Posts

Posted - 2014-07-15 : 16:01:03
This gives me the same error I was getting.. You can't use an expression in the subquery.

Error: 0x0 at Execute SQL Task: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Error: 0xC002F210 at Execute SQL Task, Execute SQL Task: Executing the query "Update EventData

Regards,
The Dark Knight
-Give What is Right, Not What is Left-
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-07-15 : 16:11:16
Sorry, try this:

Update e
Set DivTotal = t.DivTotal
from EventData ed
join (Select EventKey, SUM(EventPoints) as DivTotal
From EventData
Where eventcode = 'F'
Group by Eventkey) t
on ed.EventKey = t.EventKey

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -