Your question is SQL Joins, CTEs, and Intervals. Start with the requirements and the three tables on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Sberbank wants to identify continuous customer activity periods in Sberbank Online. Activity records can be duplicated during ingestion, and separate events belong to the same interval when the next event starts no later than one day after the running end of the current interval.
Write a PostgreSQL query that joins all three tables and returns only intervals lasting at least two calendar days and containing at least two distinct Sberbank Online activity types.
ingested_at.channel = 'Sberbank Online'.| Column | Type | Description |
|---|---|---|
| customer_idPK | INT | Unique Sberbank customer identifier |
| customer_name | VARCHAR(100) | Customer display name |
| Column | Type | Description |
|---|---|---|
| event_idPK | INT | Unique ingested event record |
| customer_id | INT | References customers.customer_id |
| activity_type_id | INT | References activity_types.activity_type_id |
| start_date | DATE | Activity interval start date |
| end_date | DATE | Activity interval end date |
| ingested_at | TIMESTAMP | Timestamp when the record was ingested |
| Column | Type | Description |
|---|---|---|
| activity_type_idPK | INT | Unique activity type identifier |
| activity_name | VARCHAR(80) | Readable activity name |
| channel | VARCHAR(40) | Sberbank product or channel where the activity occurred |