SQL Update Statement using aggregate

I need to update a field in table pocalc from data from the same record

UPDATE    POCALC
SET              Pocalc.max =ceiling(SUM(slspd01+slspd02+slspd03+slspd04+slspd05+slspd06+slspd07+slspd08+slspd09+slspd10+slspd11+slspd12)/12)
WHERE     (SiteID = 'west') AND (InvtID LIKE 'fri%')

I get the following error:
An aggregate may not appear in the set list of an UPDATE statement.

I was wondering if someone could point me in the right direction to reformatting this.
armgonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Barry CunneyCommented:
USE A SUBQUERY To get the value to fed into the 'SET = '

UPDATE    POCALC
SET              Pocalc.max =
( SELECT ceiling(SUM(slspd01+slspd02+slspd03+slspd04+slspd05+slspd06+slspd07+slspd08+slspd09+slspd10+slspd11+slspd12)/12) )
WHERE     (SiteID = 'west') AND (InvtID LIKE 'fri%')
0
armgonAuthor Commented:
thanks for the assistance.

unfortunately with the subquery I am getting this error:
Msg 157, Level 15, State 1, Line 3
An aggregate may not appear in the set list of an UPDATE statement.
0
Barry CunneyCommented:
UPDATE p
SET p.max = agg.Total
FROM POCALC p
INNER JOIN (
SELECT ID, ceiling(SUM(slspd01+slspd02+slspd03+slspd04+slspd05+slspd06+slspd07+slspd08+slspd09+slspd10+slspd11+slspd12)/12) ) Total
FROM POCALC p1
GROUP BY ID
) AS agg
ON p.ID = agg.ID
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

armgonAuthor Commented:
I am getting this error:
Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'FROM'.
0
x-menIT super heroCommented:
DECLARE @max float
set @max = select  ceiling(SUM(slspd01+slspd02+slspd03+slspd04+slspd05+slspd06+slspd07+slspd08+slspd09+slspd10+slspd11+slspd12)/12) FROM POCALC

UPDATE    POCALC
SET              Pocalc.max = @max
WHERE     (SiteID = 'west') AND (InvtID LIKE 'fri%')
0
armgonAuthor Commented:
X-men: The following error is returned with the query

Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'select'.
0
SharathData EngineerCommented:
try this.
UPDATE POCALC 
SET    Pocalc.[max] = CEILING((slspd01 + slspd02 + slspd03 + slspd04 + slspd05 
                                  + slspd06 + slspd07 + slspd08 + slspd09 + slspd10 
                                  + slspd11 + slspd12) / 12) 
WHERE  ( SiteID = 'west' ) 
   AND ( InvtID LIKE 'fri%' ) 

Open in new window

0
PortletPaulfreelancerCommented:
Like Sharath, I can't see a need for SUM()
but also, when you are dividing should you be using 12.0 ?
UPDATE POCALC
SET
    Pocalc.MAX = CEILING((slspd01 + slspd02 + slspd03 
                        + slspd04 + slspd05 + slspd06 
                        + slspd07 + slspd08 + slspd09 
                        + slspd10 + slspd11 + slspd12) / 12) --<< 12.0 ?
WHERE (SiteID = 'west')
        AND (InvtID LIKE 'fri%')
 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.