SQL · Level 2 Aggregation

SQL Monthly Reports with GROUP BY

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

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;

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.

Sample data from the first test. The other tests use different rows, so write a query, not a list of answers.

orders 6 rows
idINTEGERcreated_atTEXTchannelTEXTtotalREAL
12024-02-10 10:00web20.0
22024-02-11 11:00app15.0
32024-02-20 09:00web5.0
42024-03-01 08:00web40.0
52024-03-03 12:00app10.0
62024-03-31 23:00app2.5
solution.sql
Test Suites Pending
Output