GROUP BY · revenue
Compare monthly revenue
For each month, how much revenue did the store earn, how many copies did it sell, and how many orders did customers place?
Compare with the solution
Revenue totals €320 in October and €380 in November. The sample contains four orders in each month.
SELECT strftime('%Y-%m', p.fecha) AS mes,
COUNT(DISTINCT p.id) AS numero_pedidos,
SUM(lp.unidades) AS unidades_vendidas,
SUM(lp.unidades * lp.precio_unitario_euros) AS facturacion_euros
FROM pedidos AS p
JOIN lineas_pedido AS lp ON lp.pedido_id = p.id
GROUP BY strftime('%Y-%m', p.fecha)
ORDER BY mes ASC;| mes | numero_pedidos | unidades_vendidas | facturacion_euros |
|---|---|---|---|
| 2026-10 | 4 | 13 | 320 |
| 2026-11 | 4 | 14 | 380 |