Correlated Subquery SQL Server 2014
Add an alias for the derived table before the ;
for example VendLargestUnpaidInv:
Select Sum(LargestUnpaid) from
(Select Max(InvoiceTotal) AS LargestUnpaid from Invoices
where InvoiceTotal-(PaymentTotal+CreditTotal)<0 group by vendorID ) VendLargestUnpaidInv;
Or to make it more readable, using CTE (Common Table Expression)
With VendLargestUnpaidInv as
(
Select Max(InvoiceTotal) AS LargestUnpaid from Invoices
where InvoiceTotal - (PaymentTotal+CreditTotal)<0
group by vendorID
)
select sum(LargestUnpaid)
from VendLargestUnpaidInv;