Find the end of each signup month
Your task
Return customer_id and month_end in YYYY-MM-DD form for each signup date. Sort by customer ID.
The day before the start of next month is the last day of the current month.
Sample data
customers| id | name | signup_date | source |
|---|---|---|---|
| 1 | Avery | 2025-01-02 | organic |
| 2 | Casey | 2025-01-05 | |
| 3 | Jordan | 2025-01-12 | organic |
| 4 | Morgan | 2025-02-01 | |
| 5 | Riley | 2025-02-10 | social |
| 6 | Sam | 2025-02-20 | organic |
| 7 | Taylor | 2025-03-01 | social |
| 8 | Devon | 2025-02-07 |
Expected output
| customer_id | month_end |
|---|---|
| 1 | 2025-01-31 |
| 2 | 2025-01-31 |
| 3 | 2025-01-31 |
| 4 | 2025-02-28 |
| 5 | 2025-02-28 |
| 6 | 2025-02-28 |
| 7 | 2025-03-31 |
| 8 | 2025-02-28 |
Show a hint
Use DATE with the modifiers 'start of month', '+1 month', and '-1 day'.
Dates, text, and JSON guide
Work with dates as ISO text
The receipt dates use YYYY-MM-DD, so their text order matches calendar order. Use an inclusive start and exclusive end for periods. This query returns receipts in the first quarter, without including April 1.
SELECT id, sold_on, amount
FROM receipts
WHERE sold_on >= '2025-01-01'
AND sold_on < '2025-04-01';Normalize values before comparing them
The receipt channel values vary in capitalization and spacing. TRIM removes surrounding spaces and LOWER handles these ASCII names. Use the same normalization when grouping values that represent the same category.
SELECT LOWER(TRIM(channel)) AS channel, COUNT(*) AS receipts
FROM receipts
GROUP BY LOWER(TRIM(channel));Extract a field or expand an array
JSON_EXTRACT reads a value at a path, such as the currency code. JSON_EACH turns an array into rows with a value column. After expansion, receipt IDs can repeat and a receipt with no tags contributes no rows.
SELECT r.id, JSON_EXTRACT(r.metadata, '$.currency') AS currency,
tag.value AS tag
FROM receipts AS r
CROSS JOIN JSON_EACH(r.metadata, '$.tags') AS tag;Worked solution
SELECT id AS customer_id,DATE(signup_date,'start of month','+1 month','-1 day') AS month_end FROM customers ORDER BY id;Press Control+Space for SQL suggestions. Use the arrow keys to select an option and Enter to insert it.
Results
Loading SQLite…
