SELECT Invoices.DateSent, Invoices.Invoice_ID, patient.Patient_ID, patient.Last_Name_M0040, patient.First_Name_M0040, patient.Middle_Initial_M0040, patient.Suffix_M0040, patient.Agency_ID, Payer.PayName, Invoices.InvoiceTotal, Invoices.PayerBalance, DateDiff(CURDATE(),datesent) AS age, If(payerbalance='0','paid','unpaid') AS balance, Invoices.Deleted
FROM (Invoices INNER JOIN #Request.prefix_db_agency#.patient ON Invoices.Patient_ID = patient.Patient_ID) INNER JOIN #Request.prefix_db_agency#.Payer ON patient.Payer_ID = Payer.Payer_ID
WHERE (((patient.Patient_ID)=) AND ((Invoices.Deleted)=0))
ORDER BY Invoices.DateSent DESC;