Given a table of purchases, return the distinct customer_id values of customers who made at least one purchase in each of three consecutive calendar months M, M+1 and M+2. A run of months may cross a year boundary, so November, December and January count as consecutive.
Input Table(s)
Table purchases:
| Column | Type | Meaning |
|---|
| purchase_id | INT | Unique ID of the purchase |
| customer_id | INT | Customer who made the purchase, never NULL |
| purchase_date | DATE | Calendar date of the purchase, never NULL |
Primary key: purchase_id. A customer can have many rows, including several on the same day or in the same month.
Output Contract
-
One column,
customer_id
.
-
Each qualifying customer appears exactly once.
-
Rows are ordered by
customer_id
ascending.
-
No rounding is involved.
Example
Input purchases:
| purchase_id | customer_id | purchase_date |
|---|
| 1 | 10 | 2023-11-30 |
| 2 | 10 | 2023-12-15 |
| 3 | 10 | 2024-01-02 |
| 4 | 20 | 2024-02-01 |
| 5 | 20 | 2024-02-28 |
| 6 | 20 | 2024-03-15 |
Expected output:
Customer 10 bought in November 2023, December 2023 and January 2024, three consecutive months across a year boundary, so customer 10 qualifies. Customer 20 bought twice in February 2024 and once in March 2024, which covers only two consecutive months.
Constraints and Clarifications
-
purchases
has 1 to 100,000 rows.
-
customer_id
is a positive integer.
-
customer_id
and
purchase_date
are never NULL.
-
Several purchases in the same month count as one month of activity.
-
Consecutive is decided by calendar month alone. A purchase on November 30 and one on December 1 cover two consecutive months even though they are one day apart.
-
The three months must be adjacent. Purchases in January, February and April do not qualify.
-
A customer with a run of four or more consecutive months is listed once.