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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 no aggregate in update
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

40 Posts

Posted - 11/12/2012 :  17:54:45  Show Profile  Reply with Quote
I have an insert statement with about 40 columns in the select statement. I need to have one of the columns be filled with calculated data. I thought i would insert the data and then do an update i.e.

update Table
set column = sum(columna - columnb)

but that can not be done because aggregates are not allowed in set statement. Can I get around this without putting my 40 columns in the insert statement into the group by segment.

Thanks for the input

Flowing Fount of Yak Knowledge

4614 Posts

Posted - 11/12/2012 :  18:11:01  Show Profile  Reply with Quote
Not really sure how to advise you with the information you provided. However, I could guess that you need to use a deriuved table (or inline view) or an outter apply in order to do the update. Here is an example of one way you might do that:
    SET Column = D.SumColumn
    TableName AS T
        SELECT ID, SUM(columna - columnb) AS SumColumn
        FROM TableName
        GROUP BY Column_list
    ) AS D
    ON T.ID = D.ID

If that doesn't help, here are some links that can help you provide more information:
Go to Top of Page

Starting Member

40 Posts

Posted - 11/13/2012 :  09:52:13  Show Profile  Reply with Quote
Thank you that did the trick. New problem though the sum is not giving the correct output.

set columnc = sum(columnb - columna)

columnb = 105800 and columna = 40000 then columnc should be 65800 but it is 180440

what aggregate calculation function should i be useing

Got it fixed just used simple columnb - columna in insert statement

Edited by - seeker62 on 11/13/2012 11:00:05
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000