← Gaps and islands, sessions

Gaps longer than 45 days: 'Санкт-Петербург', premium

Level: Middle100 XP

Instructions

A client's active day is a calendar day on which at least one transaction went through any of the client's accounts (by tx_time). A gap is an unbroken run of calendar days without transactions between two consecutive active days of a client (days before the client's first and after the last active day do not count). For clients from the city 'Санкт-Петербург' with segment 'premium', find all gaps strictly longer than 45 days. Output client_id, gap_start (the first day of the gap), gap_end (the last day of the gap) and gap_days (the number of days in the gap; if the active days are March 1 and March 5, the gap lasts 3 days: March 2 to March 4). Sort by gap_days descending, then client_id, then gap_start 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