I have a database with legacy data that stores transactions and uses a "bucket" method for determining account balances. I need a way to get aged past due for accounts.
Table Transactions
TransactionId
TransactionType (CHARGE,RECEIPT)
Amount
PostDate
To get the current balance:
SELECT SUM(CASE TransactionTypeId WHEN RECEIPT THEN Amount * -1 ELSE Amount END) CurrentBalance
I need a way to determine past due 30, 60, 90, 120, etc:
Account Current 30 60 90 120+
12345 $50.00 $0.00 $25.00 $25.00 $0.00
I tried running separate queries and limiting the CHARGE postdates to greater than 30,60,90,120, running for each group and subtracting the others, etc but cannot get the expected results.
The table doesn't store a flag for past due, all balances are calculated on the fly.
Am I missing something simple? I tried a net search but not sure if there's a term for this type of sql query.
Database is SQL Server if that helps.
TIA