В новой системе OpenCart 2 появился следующий запрос отчета о продажах:
SELECT MIN(o.date_added) AS date_start,
MAX(o.date_added) AS date_end,
COUNT(*) AS orders,
( SELECT SUM(op.quantity)
FROM fc_order_product op
WHERE op.order_id = o.order_id
GROUP BY op.order_id
) AS products,
(SELECT SUM(ot.value)
FROM fc_order_total ot
WHERE ot.order_id = o.order_id
AND ot.code = 'tax'
GROUP BY ot.order_id
) AS tax,
SUM(o.total) AS total
FROM fc_order o
WHERE o.order_status_id > 0
AND DATE(o.date_added) >= '2014-12-01'
AND DATE(o.date_added) <= '2014-12-22'
GROUP BY WEEK(o.date_added)
ORDER BY o.date_added DESC
Это довольно быстро, но с ошибочными результатами в отношении налога и суммы продуктов.
Старый запрос
SELECT MIN(tmp.date_added) AS date_start,
MAX(tmp.date_added) AS date_end,
COUNT(tmp.order_id) AS orders,
SUM(tmp.products) AS products,
SUM(tmp.tax) AS tax,
SUM(tmp.total) AS total
FROM (SELECT o.order_id,
(SELECT SUM(op.quantity)
FROM fc_order_product op
WHERE op.order_id = o.order_id
GROUP BY op.order_id
) AS products,
(SELECT SUM(ot.value)
FROM fc_order_total ot
WHERE ot.order_id = o.order_id
AND ot.code = 'tax'
GROUP BY ot.order_id
) AS tax,
o.total,
o.date_added
FROM fc_order o
WHERE o.order_status_id > 0
AND DATE(o.date_added) >= '2014-12-01'
AND DATE(o.date_added) <= '2014-12-22'
GROUP BY o.order_id
) tmp
GROUP BY WEEK(tmp.date_added)
ORDER BY tmp.date_added
дает правильный результат, но занимает около 100 секунд.
Кто-нибудь пытается решить эту проблему со мной?
Задача ещё не решена.
Других решений пока нет …