You are given a subscriptions table with one row per subscription. Report month-over-month growth of active subscribers: for each reporting month, return month, active_subscribers, prev_month_subscribers and growth_pct, ordered by month ascending.
The rules:
-
A subscription is active in a month when its
start_date
is on or before the month's last day and its
end_date
is
NULL
or on or after the month's first day.
-
Report only months that appear as the month of some
start_date
, formatted
YYYY-MM
.
-
prev_month_subscribers
is the count for the previous reported month, or
NULL
if there is none.
-
growth_pct
is
(current - previous) / previous * 100
, rounded to 2 decimal places, and
NULL
when the previous count is 0 or missing.
Input Table(s)
Table subscriptions (primary key subscription_id):
| Column | Type | Meaning |
|---|
| subscription_id | INT | Subscription identifier, primary key |
| user_id | INT | Subscriber who holds the subscription, never NULL |
| start_date | DATE | First day of the subscription, never NULL |
| end_date | DATE | Last day of the subscription, NULL while it is ongoing |
Output Contract
One row per reporting month:
| Column | Type | Meaning |
|---|
| month | TEXT | Reporting month as YYYY-MM |
| active_subscribers | INTEGER | Number of subscriptions active at any point in that month |
| prev_month_subscribers | INTEGER | active_subscribers of the previous reported month, NULL for the first one |
| growth_pct | NUMERIC | (active_subscribers - prev_month_subscribers) / prev_month_subscribers * 100, rounded to 2 decimal places, NULL when the previous count is NULL or 0 |
Order by month ascending. Each month appears once, so the order is fully determined.
Example
subscriptions:
+-----------------+---------+------------+------------+
| subscription_id | user_id | start_date | end_date |
+-----------------+---------+------------+------------+
| 1 | 10 | 2024-01-05 | 2024-01-31 |
| 2 | 11 | 2024-01-10 | 2024-02-15 |
| 3 | 12 | 2024-01-25 | NULL |
| 4 | 13 | 2024-02-01 | 2024-02-20 |
| 5 | 14 | 2024-02-28 | 2024-03-31 |
| 6 | 15 | 2024-04-05 | NULL |
| 7 | 15 | 2024-04-18 | 2024-04-30 |
+-----------------+---------+------------+------------+
Expected result:
+---------+--------------------+------------------------+------------+
| month | active_subscribers | prev_month_subscribers | growth_pct |
+---------+--------------------+------------------------+------------+
| 2024-01 | 3 | NULL | NULL |
| 2024-02 | 4 | 3 | 33.33 |
| 2024-04 | 3 | 4 | -25.00 |
+---------+--------------------+------------------------+------------+
No subscription starts in March, so March is not reported and April is compared with February. January has subscriptions 1, 2 and 3. February has 2, 3, 4 and 5 (subscription 1 ended on January 31). April has 3, 6 and 7, so it counts 3 even though subscriptions 6 and 7 belong to the same user.
Constraints and Clarifications
-
The table has 1 to 100,000 rows, and
start_date <= end_date
whenever
end_date
is not
NULL
.
-
Both dates are inclusive. A subscription that is active for a single day of a month counts for that month.
-
The count is of subscriptions. A user who holds two subscriptions active in the same month counts twice.
-
A calendar month in which no subscription starts is skipped even if subscriptions are active during it. The next reported month then uses the last reported month before the gap as its previous month.
-
growth_pct
can be negative. Compute it with decimal arithmetic and round only the final value.
-
subscription_id
is unique, so there are no duplicate rows to remove.