AI Reports

Magento 2 SQL queries › Customers

Which customer group has the highest average order value?

Short answer: Every order stores the customer group it was placed in. Average the totals per group.

In the Magento admin

  1. Export orders from Sales › Orders with the Customer Group column and average per group 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 COALESCE(g.customer_group_code, CONCAT('group ', o.customer_group_id)) AS customer_group,
       COUNT(*) AS orders,
       ROUND(AVG(o.base_grand_total), 2) AS avg_order_value
FROM sales_order o
LEFT JOIN customer_group g ON g.customer_group_id = o.customer_group_id
WHERE o.state <> 'canceled'
  AND o.created_at >= CURRENT_DATE - INTERVAL 12 MONTH
GROUP BY o.customer_group_id, g.customer_group_code
ORDER BY avg_order_value 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