AI Reports

Magento 2 SQL queries › Orders and sales

What was our revenue per website last month, excluding canceled orders?

Short answer: The Sales report can show one website at a time after you refresh its statistics. One query gives every website side by side, with refunds in their own column.

In the Magento admin

  1. Go to Reports › Statistics › Refresh Statistics, select the Orders report and refresh it.
  2. Open Reports › Sales › Orders, set the period to last month and Order Status to Specified without Canceled.
  3. Switch the scope at the top to each website in turn.

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,
       COUNT(*) AS orders,
       ROUND(SUM(o.base_grand_total), 2) AS revenue,
       ROUND(SUM(IFNULL(o.base_total_refunded, 0)), 2) AS refunded
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 o.created_at >= DATE_FORMAT(CURRENT_DATE - INTERVAL 1 MONTH, '%Y-%m-01')
  AND o.created_at <  DATE_FORMAT(CURRENT_DATE, '%Y-%m-01')
GROUP BY w.website_id, w.name
ORDER BY revenue 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