Calculate Age of Debt in SQL Server
I have three tables:
Charges
Payments
Adjustments
Each has a value, called Amount. There are no allocations done on this data, we are assuming the oldest Payments are paying the oldest Charges or Adjustments. Each Amount could be +ve or -ve.
I need to produce a report which shows the age of the debt, based on the current balance being in debt, where the balance is the sum of each Amount in all tables. However, the age of the debt must be the Age of the current debt. If an account was in debit in October, but was zeroed in November and then in Debit in February, the Age of the Debt would be February. In need to provide a 30, 60, 90 day breakdown of each account whose balance is outstanding.
Sorry if this isn't clear, but if you've done it before you'll know what I开发者_运维技巧 mean. Any pointers?
Just been playing with a pen and paper. Is this as simple as:
Amnt Current Debt at Time = Sum(Debits to Time) - Sum (All Credits)
So in SQL: Convert All +ve Charges, -ve Adjustments or -ve Payments to Debits (UNION) Convert all -ve Charges, +ve Adjustments or +ve Payments to Credits (UNION) For each of your age points, get the Sum of Debits to that point and subtract all of the credits for all time. (SUM and GROUP BY)
Any problems with this?
精彩评论