← Practice

Consecutive salary months (3+): Kazan, threshold 20000

Level: SeniorTopic: Gaps and islands, sessions200 XP

Instructions

A client's salary month is a calendar month in which the sum of their transactions of the category 'salary' (across all accounts, by tx_time) is at least 20000. For clients from the city 'Казань', find all runs of at least 3 salary months in a row (no missing months between them). Output client_id, first_month and last_month (the first days of the first and last month of the run, type date), months (the number of months in the run) and salary_total (the sum of the salary transactions over those months). Sort by months descending, then client_id, then first_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