WITH · AVG

Calculate average order value

What is the average order value if you count each order just once?

SQLite
RESULTSReady to run
Compare with the solution

The eight receipts total €700, so the average order value is €87.50. Averaging the 15 individual order lines would give a different answer.

WITH totales_por_pedido AS (
    SELECT p.id AS pedido_id,
           SUM(lp.unidades * lp.precio_unitario_euros) AS total_euros
    FROM pedidos AS p
    JOIN lineas_pedido AS lp ON lp.pedido_id = p.id
    GROUP BY p.id
)
SELECT ROUND(AVG(total_euros), 2) AS ticket_medio_euros
FROM totales_por_pedido;
ticket_medio_euros
87.5