За да преброите общите поръчки, направени месечно през последната година от определен клиент, използвайте тази заявка:
SELECT MONTHNAME(c.datefield) AS Month,
YEAR(CURRENT_DATE - INTERVAL 1 YEAR) AS Year,
IFNULL(o.TotalOrders, 0) AS Orders
FROM calendar AS c
LEFT JOIN (
SELECT MONTH(o.order_date) AS Month,
YEAR(o.order_date) AS Year,
COUNT(o.customer_id) AS TotalOrders
FROM orders AS o
WHERE YEAR(o.order_date) = YEAR(CURRENT_DATE - INTERVAL 1 YEAR) AND o.customer_id = 329
GROUP BY Month) AS o
ON MONTH(c.datefield) = o.Month
GROUP BY MONTH(c.datefield)