新しい 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 秒かかります。誰かが私と一緒にこの問題を解決しようとしていますか?