Report high-value order share across all months
Your task
For January through April 2025, return month in YYYY-MM format, completed_orders, high_value_orders of at least 3000 cents, and high_value_pct. Show zero counts and NULL percentage for empty months. Round to one decimal and sort chronologically.
A reporting calendar keeps empty months visible and makes the missing denominator explicit.
Sample data
shop_orders| id | customer_id | ordered_on | channel | status | total_cents |
|---|---|---|---|---|---|
| 201 | 1 | 2025-01-04 | online | completed | 2500 |
| 202 | 2 | 2025-01-10 | store | completed | 1800 |
| 203 | 2 | 2025-01-10 | store | completed | 3200 |
| 204 | 3 | 2025-01-18 | marketplace | refunded | 5000 |
| 205 | 4 | 2025-02-02 | store | completed | 2700 |
| 206 | 1 | 2025-02-04 | online | completed | 4000 |
| 207 | 5 | 2025-02-18 | online | cancelled | 2400 |
| 208 | 6 | 2025-02-28 | online | completed | 1700 |
| 209 | 4 | 2025-03-16 | online | completed | 2900 |
| 210 | 8 | 2025-03-09 | store | completed | 3100 |
Expected output
| month | completed_orders | high_value_orders | high_value_pct |
|---|---|---|---|
| 2025-01 | 3 | 1 | 33.3 |
| 2025-02 | 3 | 1 | 33.3 |
| 2025-03 | 2 | 1 | 50 |
| 2025-04 | 0 | 0 | NULL |
Show a hint
Generate month starts, left join each month’s completed orders, and divide the qualifying count by NULLIF of the order count.
Optimization and capstones guide
Compare equivalent filters
A function on an indexed column can hide the key from a direct range lookup. The two predicates here select the same January dates in this dataset. Compare the plans and look for a constrained SEARCH on the second query.
EXPLAIN QUERY PLAN
SELECT id FROM receipts WHERE STRFTIME('%Y-%m', sold_on) = '2025-01';
EXPLAIN QUERY PLAN
SELECT id FROM receipts
WHERE sold_on >= '2025-01-01' AND sold_on < '2025-02-01';Continue from a stable key
OFFSET skips entries before the requested page. With keyset pagination, use the last ID from the previous page as a boundary. The order and boundary must use the same key. Use a unique tie-breaker if the leading sort column can repeat.
EXPLAIN QUERY PLAN
SELECT id, title FROM catalog
WHERE id > 500
ORDER BY id
LIMIT 5;Summarize first, compare second
Start reports by deciding what one result row represents. This report groups receipts by month, then compares monthly totals with LAG. If February has no receipts, the next row compares with January. Generate a calendar CTE and left join it when gaps need to appear explicitly.
WITH monthly AS (
SELECT STRFTIME('%Y-%m', sold_on) AS month, SUM(amount) AS total
FROM receipts
GROUP BY month
)
SELECT month, total, total - LAG(total) OVER (ORDER BY month) AS change
FROM monthly
ORDER BY month;Worked solution
WITH RECURSIVE months(day) AS (VALUES('2025-01-01') UNION ALL SELECT DATE(day,'+1 month') FROM months WHERE day<'2025-04-01') SELECT STRFTIME('%Y-%m',m.day) AS month,COUNT(o.id) AS completed_orders,SUM(CASE WHEN o.total_cents>=3000 THEN 1 ELSE 0 END) AS high_value_orders,ROUND(100.0*SUM(CASE WHEN o.total_cents>=3000 THEN 1 ELSE 0 END)/NULLIF(COUNT(o.id),0),1) AS high_value_pct FROM months m LEFT JOIN shop_orders o ON o.ordered_on>=m.day AND o.ordered_on<DATE(m.day,'+1 month') AND o.status='completed' GROUP BY m.day ORDER BY m.day;Press Control+Space for SQL suggestions. Use the arrow keys to select an option and Enter to insert it.
Results
Loading SQLite…
