I have a database with outdated data that stores transactions and uses the "bucket" method to determine account balances. I need a way to get past due for bills.
Operations with tables
TransactionID Transaction
type (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 30, 60, 90, 120, etc .:
Current account 30 60 90 120+
12345 $ 50.00 $ 0.00 $ 25.00 $ 25.00 $ 0.00
I tried to execute individual queries and limited the CHARGE postdates to more than 30,60,90,120, performing for each group and subtracting the rest, etc., but could not get the expected results.
The table does not store the delay flag, all balances are calculated on the fly.
Am I missing something simple? I tried network search, but not sure if there is a term for this type of sql query.
A database is SQL Server if that helps.
TIA
sql_q source
share