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
- Go to Reports › Statistics › Refresh Statistics, select the Orders report and refresh it.
- Open Reports › Sales › Orders, set the period to last month and Order Status to Specified without Canceled.
- 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
base_grand_totalis in your base currency, so websites with different display currencies still add up correctly.- If you use one base currency per website, compare websites in their own currency, or convert first.