← Practice

Balance of account 1040 at 1.5% per month from January 2025

Level: SeniorTopic: Recursive CTEs200 XP

Instructions

Model a savings account from the transactions of the account with account_id = 1040 over 6 months: from January 1, 2025 to the end of the month that starts on June 1, 2025. The balance before the first month is 0. In each month interest is applied first: the balance at the end of the previous month is multiplied by 1.015 (1.5% per month, also when the balance is negative), then flow is added, the sum of amount over all transactions of this account in the month (signed; 0 if there were none). Output month (the first day of the month, type date), flow and balance (the balance at the end of the month, rounded to 2 decimal places; round only the result, not the intermediate balances). Sort by month ascending.

Hints

Each next one is more detailed and costs more

Log in to open hints

Query

Result

Click Run to see the query result, or Check to submit your solution.

Dataset schema

accounts.client_id (0…N) ↔ clients.id (1…1)transactions.account_id (0…N) ↔ accounts.id (1…1)accountsPKidintegerFKclient_idintegerclientsPKidintegertransactionsPKidintegerFKaccount_idinteger
Exactly oneZero or oneZero or more
Table relationships
  • N:1Many-to-oneaccounts.client_id → clients.id
  • N:1Many-to-onetransactions.account_id → accounts.id

Table data

PostgreSQL

Select a table in the diagram or list to view its rows.

Dataset schema