T-SQL - UPDATE table with the sum of columns in 2 other tables

I have 3 tables. A column in table1 needs to be updated the sum of a column that exists in both of the other 2 tables.

update table1
      set
            aFee = sum(t2.aFee + t3.aFeeADJ)
      from
            table2 t2
                  Join table3 t3 on t3.ID = t2.ID
      where
            table1.ID = t2.ID
            and table1.column1 = 'Stuff'
            and table1.column2 in (1,2)

Result:
Msg 157, Level 15, State 1, Line 139
An aggregate may not appear in the set list of an UPDATE statement.

Thank you in advance for any assistance!
RickzzzAsked:
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.

Éric MoreauSenior .Net ConsultantCommented:
your complete query is incorrect! You cannot update a table (Table1 in your case) if it is not in the FROM clause!

give a try to :
update t1
set aFee = t2.aFee + t3.aFeeADJ
from table1 as T1
inner join table2 t2
on t2.ID  = t1.ID
inner Join table3 t3 
on t3.ID = t2.ID
where t1.column1 = 'Stuff'
and table1.column2 in (1,2)

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
RickzzzAuthor Commented:
Thanks, that did it.

One issue though, there isn't always a record in t3 that exists in t1 and t2, therefore instead of the updating with just the value in t2, I get a NULL. Basically, t3 is a much smaller table with any adjustments to the fee in t2 that MAY exist.

Can you assist there as well?
0
ste5anSenior DeveloperCommented:
Use a LEFT JOIN and ISNULL:

UPDATE T1
SET    aFee = T2.aFee + ISNULL(T3.aFeeADJ, 0)
FROM   table1 T1
       INNER JOIN table2 T2 ON T2.ID = T1.ID
       LEFT JOIN table3 T3 ON T3.ID = T2.ID
WHERE  T1.column1 = 'Stuff'
       AND table1.column2 IN ( 1, 2 );

Open in new window

0
Éric MoreauSenior .Net ConsultantCommented:
much like ste5an said:

update t1
set aFee = t2.aFee + isnull(t3.aFeeADJ,0)
from table1 as T1
inner join table2 t2
on t2.ID  = t1.ID
left Join table3 t3 
on t3.ID = t2.ID
where t1.column1 = 'Stuff'
and t1.column2 in (1,2)

Open in new window

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
T-SQL

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.