AI Reports

Magento 2 SQL queries › Orders and sales

Which payment methods were used last month, and how many orders each?

Short answer: The order grid shows the payment method per order but can't group them. The payment method lives in sales_order_payment; join it to the orders and count.

In the Magento admin

  1. Go to Sales › Orders, add the Payment Method column from Columns and filter by last month.
  2. Export the grid to CSV and count the values in a spreadsheet.

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 p.method, COUNT(*) AS orders, ROUND(SUM(o.base_grand_total), 2) AS base_total
FROM sales_order o
JOIN sales_order_payment p ON p.parent_id = o.entity_id
WHERE 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 p.method
ORDER BY orders 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