Overview
Build a revenue report per month and sales channel: group by a calculated month and by a second column, without mixing up years.
- GROUP BY an expression
- Grouping by two columns
- substr() on ISO dates
Short Course
You can group by a calculation: substr(created_at, 1, 7) turns
'2024-03-15 10:00' into '2024-03'. Trap: every selected column must be
grouped or aggregated. SQLite quietly shows a value from a random row;
PostgreSQL refuses the query.
SELECT substr(created_at, 1, 7) AS month, COUNT(*) AS orders
FROM orders
GROUP BY month
ORDER BY month;
Task
Return the revenue (SUM(total)) for each month ('2024-03') and channel,
ordered by month, then channel. In the database below, the web channel made 25
in February 2024.
Database
Sample data from the first test. The other tests use different rows, so write a query, not a list of answers.
| idINTEGER | created_atTEXT | channelTEXT | totalREAL |
|---|---|---|---|
| 1 | 2024-02-10 10:00 | web | 20.0 |
| 2 | 2024-02-11 11:00 | app | 15.0 |
| 3 | 2024-02-20 09:00 | web | 5.0 |
| 4 | 2024-03-01 08:00 | web | 40.0 |
| 5 | 2024-03-03 12:00 | app | 10.0 |
| 6 | 2024-03-31 23:00 | app | 2.5 |
Test Suites
Pending
Next
Output