SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

seeker62
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

Lamprey
Flowing Fount of Yak Knowledge

4353 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:
UPDATE T
    SET Column = D.SumColumn
FROM 
    TableName AS T
INNER JOIN
    (
        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:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

seeker62
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)

if
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  
 New 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.05 seconds. Powered By: Snitz Forums 2000