I am guessing that you are aging invoices against payments. Basically, you gather the invoices, and you gather the payments for each invoice, and subtract the payments from the invoice. what is left over is what is being aged. Usually, the age is from the day the invoice was issued or past due, and you would count the days. If the invoice is paid in full by the payments, it drops off the aging as of its paid date. If it is overpaid, it would still be on the aging, but as a credit to the customer...as would payments that have been received but not applied any particular invoice.
There are other minutae associated with aging, but that is the fundamentals. I use stored procedures to create the table and figure out how much is owed as the process is usually more than can be done in 1 select statement, and that is CR's limit.
HTH