Вы можете присоединиться:
SELECT o.count_orders, b.*
FROM (
SELECT count(*) AS Count_Boxes,
DATE_FORMAT(create_date, '%b') AS mn,
DATE_FORMAT(create_date, '%Y') AS yr
FROM boxes
GROUP BY YEAR(create_date), MONTH(create_date)
) b
INNER JOIN (
SELECT count(*) as Count_Orders,
DATE_FORMAT(create_date, '%b') AS mn,
DATE_FORMAT(create_date, '%Y') AS yr
FROM orders
GROUP BY YEAR(create_date), MONTH(create_date)
) o ON o.mn = b.mn and o.yr = o.yr
Однако, если существует вероятность пропуска периода в любом из наборов данных, тогда другой подход использует union all
:
SELECT
SUM(no_orders) count_orders,
SUM(no_boxes) count_boxes,
DATE_FORMAT(create_date, '%b') mn,
DATE_FORMAT(create_date, '%Y') yr
FROM (
SELECT create_date, 0 no_orders, 1 no_boxes FROM boxes
UNION ALL
SELECT create_date, 1, 0 FROM orders
)
GROUP BY YEAR(create_date), MONTH(create_date)