← Back to track

Active-week streaks: 'Новосибирск', mass, all transactions

Level: SeniorTopic: Gaps and islands, sessions200 XP

Instructions

A week is 7 days from Monday to Sunday (as in date_trunc('week', …)); a week is active for a client if at least one transaction went through in it on any of the client's accounts. A streak is a maximal run of consecutive active weeks with no missing weeks (it may consist of a single week). For each client from the city 'Новосибирск' with segment 'mass' who has at least one active week, output client_id, active_weeks (the number of active weeks), streaks (the number of streaks) and longest_streak (the length of the longest streak in weeks). Sort by longest_streak descending, then active_weeks descending, then client_id 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