← Back to track

Activity periods of client 314, pause at most 6 days

Level: SeniorTopic: Gaps and islands, sessions200 XP

Instructions

Split the activity of the client with client_id = 314 in 2025 into periods. An active day is a day on which at least one transaction went through any of the client's accounts (by tx_time). The pause between two consecutive active days is the number of days without transactions between them (between March 3 and March 5 the pause is 1 day, between March 3 and March 4 it is 0). A pause of at most 6 days does not break a period; a pause longer than 6 days starts a new period. Output period_no (the period number: 1, 2, … in time order), period_start and period_end (the first and last active day of the period), active_days (the number of active days in it) and tx_cnt (the number of transactions in it). Sort by period_no.

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