Every now and then you may attempt to calculate an aggregate function -- such as SUM() -- on a correlated subquery, only to encounter the following error:
Msg 130, Level 15, State 1, Line 24Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
This can be a tricky situation to resolve depending on how your query is structured and how complicated it is, but here's a couple of ideas to help you out.
Read Aggregating Correlated Sub-Queries