Date Series Gap-Fill with Event Counts
Preview mode. Log in to edit, run, submit, and save progress.
Date Series Gap-Fill with Event Counts
You are given an Events table and a DateRange table with a single row containing a start and end date. Write a SQL query that generates every calendar date in [start_date, end_date] using a recursive CTE, and for each date reports the total event count, click count, and view count - even for dates with no events (showing 0s). Return (event_date, total_events, clicks, views) ordered by event_date. Table: Events
| Column Name | Type | Description |
|---|---|---|
| event_id | INT | Primary key |
| event_date | DATE | Date of the event |
| event_type | VARCHAR | Either 'click' or 'view' |
Table: DateRange
| Column Name | Type | Description |
|---|---|---|
| start_date | DATE | Range start |
| end_date | DATE | Range end |
Examples
Generate the full date range with a recursive CTE. LEFT JOIN events to preserve gap dates. Use conditional aggregation to split clicks vs views.
Approach hint
Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.
Common mistake
Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.