I am (unsuccessfully) using a correlated subquery on an Invoices table:
Invoices(InvoiceID, VendorID, InvoiceTotal, PaymentTotal, CreditTotal,.... ),
to find the sum of largest unpaid invoices by all vendors, where the unpaid condition is given by
Select Sum(LargestUnpaid) from (Select Max(InvoiceTotal) AS LargestUnpaid from Invoices where InvoiceTotal-(PaymentTotal+CreditTotal)<0 group by vendorID ) ;
The inner query runs , which is good and bad, since the query is supposed to be correlated , but the query as a whole does not run, and I get the error message:
Msg 102, Level 15, State 1, Line 4 Incorrect syntax near ')'.
What am I doing wrong?