← Subqueries

Новосибирск, debit: spend above avg x1.5, Feb–Nov 2025

Level: Middle100 XP

Instructions

For each account and calendar month, compute the total spending spent: the sum of -amount over transactions with amount < 0 whose tx_time is on or after February 1, 2025 and strictly before December 1, 2025 (months in which an account has no spending are not considered). An account's average monthly spending is the average of spent over its considered months. For accounts of type 'debit' in currency 'RUB' of clients from the city 'Новосибирск' (clients.city), output account_id, month (the first day of the month) and spent for the months in which spent is strictly greater than the same account's average monthly spending, multiplied by 1.5. Row order does not matter.

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