A customer's 90-day LTV is the sum of amount over their orders in [first_date, first_date + 90 days), where first_date is their earliest order_date. Group customers by cohort_month (YYYY-MM of first_date) and return cohort_month, new_customers, avg_ltv_90d and total_ltv_90d (both rounded to 2 decimals), ordered by cohort_month ascending.
Input Table(s)
Table orders (primary key order_id, all columns NOT NULL):
| Column | Type | Meaning |
|---|
| order_id | INT | Identifier of the order |
| customer_id | INT | Customer who placed the order |
| order_date | DATE | Date of the order |
| amount | DECIMAL(10,2) | Order value; non-negative |
Output Contract
One row per cohort month that has at least one new customer, with these columns in this order:
| Column | Definition |
|---|
| cohort_month | first_date formatted as YYYY-MM (text) |
| new_customers | Number of customers whose first_date falls in this month |
| avg_ltv_90d | Average 90-day LTV of those customers, rounded to 2 decimals |
| total_ltv_90d | Sum of the 90-day LTV of those customers, rounded to 2 decimals |
Sort by cohort_month ascending.
Example
orders:
| order_id | customer_id | order_date | amount |
|---|
| 1 | 1 | 2023-06-01 | 100.00 |
| 2 | 1 | 2023-08-29 | 40.00 |
| 3 | 1 | 2023-08-30 | 25.00 |
| 4 | 2 | 2023-06-15 | 60.00 |
Expected output:
| cohort_month | new_customers | avg_ltv_90d | total_ltv_90d |
|---|
| 2023-06 | 2 | 100.00 | 200.00 |
Customer 1 first purchased on 2023-06-01, so the window ends before 2023-08-30. Order 2 (2023-08-29) is inside, and order 3 (2023-08-30) is exactly 90 days later and excluded, so the LTV is 140.00. Customer 2 is also in cohort 2023-06, with an LTV of 60.00. The cohort has 2 customers, an average of 100.00 and a total of 200.00.
Constraints and Clarifications
-
orders
has 1 to 10,000 rows, and
amount
is non-negative.
-
No column is NULL.
-
The window is half-open: an order counts when
order_date >= first_date
and
order_date < first_date + 90 days
, so day 0 through day 89 are inside.
-
Every order row counts separately, including several orders on the same day. All orders on
first_date
are inside the window.
-
A customer belongs to exactly one cohort, the month of their first order. Later orders in other months do not create new cohort rows.
-
Use the data as given. A customer whose 90-day window runs past the last date in the table keeps the partial total, with no adjustment.