The situation
Each row of coverage_periods says a user had an active plan from one date to
another. Renewals, upgrades and re-sent records mean the periods of one user overlap,
sit inside each other, or follow each other without a break.
Billing needs the continuous stretches: from when to when was the user covered without interruption?
Input tables
coverage_periods
One row per recorded period. A user has one or more rows.
| Column | Type | Description |
|---|---|---|
user_id |
BIGINT | The user. |
start_date |
DATE | First covered day. |
end_date |
DATE | Last covered day (inclusive). Never before start_date. |
Requirement
Merge each user's periods into continuous blocks and return one row per block.
- Two periods belong to the same block if they overlap, or if one starts the day after the other ends. Covered up to 31 January and again from 1 February is continuous cover.
- One or more uncovered days between two periods separate them into different blocks.
- A period that lies completely inside another one changes nothing.
- A block's
start_dateis its first covered day and itsend_dateis its last covered day.
Expected output
One row per continuous block.
| Column | Type | Description |
|---|---|---|
user_id |
BIGINT | |
start_date |
DATE | First day of the block. |
end_date |
DATE | Last day of the block. |
Solve it in the browser
This editor runs DuckDB inside your browser. Nothing is installed and nothing is sent to a server. DuckDB's SQL is close to Spark SQL but not identical. The Colab notebook runs real Spark.
Your result appears here. Your draft is kept in this browser.
Explanation
Show explanationGives the answer away. Try the problem first.
The approach
Sort each user's periods by start_date and walk through them. A period either
continues the block that is already open or starts a new one. It starts a new one only
if it begins after everything seen so far has ended.
"Everything seen so far" is the important phrase. The question is not "did the
previous period end before this one starts?" but "has any earlier period reached
this far?" That is a running maximum of end_date over all earlier rows:
MAX(end_date) OVER (
PARTITION BY user_id
ORDER BY start_date, end_date
ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
) AS covered_until
The frame stops at 1 PRECEDING, so the current row is not included. For a user's
first period the frame is empty and covered_until is NULL.
A period starts a new block when:
covered_until IS NULL OR start_date > DATE_ADD(covered_until, 1)
The + 1 implements the rule that cover up to 31 January followed by cover from
1 February is continuous. Dates are inclusive on both ends, so "the next day" is
adjacent, not a gap.
After that it is the usual gaps-and-islands finish: a running sum of the "starts a new
block" flag numbers the blocks, and a GROUP BY user_id, block with MIN(start_date)
and MAX(end_date) produces one row per block.
The 81 periods of 30 users merge into 52 blocks.
Common wrong answers
LAG(end_date) instead of the running maximum. Returns 54 rows. It works until a
short period sits inside a long one. User 503:
| start_date | end_date | |
|---|---|---|
| 1 Jan | 31 Mar | long period |
| 10 Jan | 20 Jan | inside the first |
| 15 Mar | 30 Apr | overlaps the first, not the second |
For the third row, LAG(end_date) is 20 January, so it looks like a new block starts
on 15 March. In fact the first period is still running. This is the mistake that
separates people who have memorised gaps-and-islands from people who understand it.
start_date > covered_until without the extra day. Returns 56 rows. Periods that
follow each other without a gap (user 507) are left as separate blocks.
Being too generous with the gap. Allowing start_date <= covered_until + 2
returns 49 rows. User 511 has no cover on 1 February; that one day is a real
interruption.
MIN(start_date) and MAX(end_date) per user. Returns 30 rows. It reports every
user as covered from their first day to their last, gaps included.
SELECT DISTINCT. Removes the one repeated period and merges nothing: 80 rows.
Edge cases in the data
| Case | Users | What it catches |
|---|---|---|
| Period inside a longer one, followed by an overlap with the long one | 503, 527 | LAG(end_date). |
| Next period starts the day after the previous ends | 507 | Missing the + 1. |
| Exactly one uncovered day | 511 | Merging across a real gap. |
| The same period twice | 515 | Duplicates creating an extra block. |
| Same start date, different end dates | 519 | Ordering by start_date alone and assuming the first row is the longest. |
| Chain: A overlaps B, B overlaps C, A does not reach C | 523 | Pairwise self-joins that merge only direct neighbours. |
| Single period | 530 | Empty frame: covered_until is NULL. |