Update fields with SUM from a table - Help

Hello guys,

I tried to run this script but it complains:

 An aggregate may not appear in the set list of an UPDATE statement.

Could you give me a help on this?

Thanks a lot


update arfin set arfin.VALBAX=sum(bx.VALINI), arfin.VALJUR=sum(bx.VALJUR), arfin.VALDESC=sum(bx.VALDESC), 
                 arfin.VALOUTROS=sum(bx.VALOUTROS), arfin.VALMULTA=sum(bx.VALMULTA) from arfin_detalhe bx
       where arfin.codcad=bx.codcad and arfin.numdco= bx.numdco and arfin.codempresa = bx.codempresa
and bx.codcad = 7 and bx.numdco = '06042018-01'  and bx.codempresa ='E001'

Open in new window

LVL 1
hidrauAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Kevin CrossChief Technology OfficerCommented:
Use a derived table as the source instead.

update a 
set a.VALBAX=bx.VALINI /* plus other SETs */
from arfin a
join (
   select  codcad, numdco, codempresa, valini = sum(VALINI) /* plus other sums */
   from arfin_detalhe 
   where codcad = 7 and numdco = '06042018-01'  and codempresa ='E001'
   group by codcad, numdco, codempresa
) bx on a.codcad=bx.codcad and a.numdco= bx.numdco and a.codempresa = bx.codempresa
;

Open in new window

1
Mark WillsTopic AdvisorCommented:
Or can use a CTE (Common Table Expression)
;with CTE_Update as
(  select codcad, numdco, codempressa
          , sum(VALINI) as VALINI
          , sum(VALJUR) as VALJUR
          , sum(VALDESC) as VALDESC
          , sum(VALOUTROS) as VALOUTROS
          , sum(VALMULTA) as VALMULTA
   from arfin_detalhe 
   where codcad = 7 
   and numdco = '06042018-01'  
   and codempresa ='E001'
   group by codcad, numdco, codempressa    
)  
   update a set VALBAX=C.VALINI
               ,VALJUR=C.VALJUR
               ,VALDESC=C.VALDESC 
               ,VALOUTROS=C.VALOUTROS
               ,VALMULTA=C.VALMULTA 
   from arfin a
   inner join CTE_Update C on C.codcad = a.codcad, C.numdco = a.numdco, C.codempresa = a.codempresa

Open in new window

But test first by doing a select * from arfin a inner join CTE_Update .....
2

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
hidrauAuthor Commented:
thanks a lot
0
hidrauAuthor Commented:
thanks a lot
0
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 2008

From novice to tech pro — start learning today.