AI Reports

Magento 2 SQL queries › Orders and sales

Compare orders this October with October last year, per website.

Short answer: Run the Sales report twice and compare by hand, or use one query that puts both years side by side for each website. Change the month number to compare a different month.

In the Magento admin

  1. Refresh statistics, then open Reports › Sales › Orders.
  2. Run it for October this year, then for October last year, per website scope, and compare.

With one query

These queries only read data. Run them on a copy of your database if you can, or ask your developer to. If your tables have a prefix (set in app/etc/env.php), add it to each table name.

Works on Magento Open Source and Adobe Commerce:

SELECT COALESCE(w.name, '(deleted store)') AS website,
       SUM(YEAR(o.created_at) = YEAR(CURRENT_DATE))     AS orders_this_year,
       SUM(YEAR(o.created_at) = YEAR(CURRENT_DATE) - 1) AS orders_last_year,
       ROUND(SUM(IF(YEAR(o.created_at) = YEAR(CURRENT_DATE),     o.base_grand_total, 0)), 2) AS revenue_this_year,
       ROUND(SUM(IF(YEAR(o.created_at) = YEAR(CURRENT_DATE) - 1, o.base_grand_total, 0)), 2) AS revenue_last_year
FROM sales_order o
LEFT JOIN store s ON s.store_id = o.store_id
LEFT JOIN store_website w ON w.website_id = s.website_id
WHERE o.state <> 'canceled'
  AND MONTH(o.created_at) = 10
  AND YEAR(o.created_at) IN (YEAR(CURRENT_DATE), YEAR(CURRENT_DATE) - 1)
GROUP BY w.website_id, w.name
ORDER BY revenue_this_year DESC;

Good to know

Related

Gaurav Harsh

Gaurav Harsh is a Magento developer who works on large Adobe Commerce stores and builds AI Reports, a Magento extension for asking your store's data questions in plain English. LinkedIn