How Long Do Churned Deals Last?
Loopwire is building its first churn model — churn being a customer stopping their subscription — and the modeller's first request is the plainest possible training input: for every contract that has already ended, how many days it lasted from the day it began to the day it stopped. Contracts still running have no answer yet and must stay out of the file, since a half-lived contract entered as a completed one would teach the model that customers leave sooner than they do.
subscriptions — one row per contract. account_id points at a row in accounts. plan is the tier the customer bought. mrr is what that contract bills every month, in dollars. start_date is the day it began. A contract that has stopped carries the day it stopped in end_date; one that is still running has nothing there, shown as — below and stored as NULL.
| id | account_id | plan | mrr | start_date | end_date |
|---|---|---|---|---|---|
| 1 | 1 | starter | 49 | 2022-11-03 | 2023-02-03 |
| 2 | 1 | growth | 149 | 2023-02-03 | — |
| 3 | 2 | growth | 149 | 2022-12-15 | 2023-06-15 |
| 4 | 3 | starter | 49 | 2023-01-31 | — |
| 5 | 4 | growth | 149 | 2023-02-06 | — |
| 6 | 5 | enterprise | 499 | 2023-02-22 | — |
| 7 | 6 | starter | 49 | 2023-03-01 | 2023-04-01 |
| 8 | 7 | growth | 149 | 2023-03-22 | — |
| 9 | 8 | enterprise | 499 | 2023-04-28 | — |
| 10 | 2 | starter | 49 | 2023-06-15 | — |
The table already exists in the database — there is nothing to create or load.
Task: Write a query that returns two columns, id and lifetime_days, one row for every contract that has stopped, lifetime_days being the whole number of days between its start date and its end date. Longest-lived first; equal lifetimes are listed with the smaller id first.
Example output
Shape only — these contracts and spans are invented:
| id | lifetime_days |
|---|---|
| 44 | 420 |
| 31 | 275 |
| 58 | 63 |
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in