Magento 2 require to create SQL query for the custom report
-
27-03-2021 - |
Question
I would like to generate Magento native report as follows:
Can anybody provide the SQL query for above table ?
- Column Name CustomerGender = customer attribute
- Column Name Group = Customer Group native feature
- Grand Total = Grand total from Sales order table
Went through plenty of the links but got regular simple joins.
Solution 2
I managed to get the results using below SQL query.
SELECT eav_attribute_option_value.value as 'Gender',
customer_group.customer_group_code as 'Group',
CONCAT('$', FORMAT(SUM(sales_order.`grand_total`),2)) AS 'Grand total'
FROM `customer_grid_flat`
LEFT JOIN
customer_group ON customer_grid_flat.group_id = customer_group.customer_group_id
LEFT JOIN
sales_order ON customer_grid_flat.entity_id = sales_order.customer_id
LEFT JOIN
eav_attribute_option_value ON customer_grid_flat.gender = eav_attribute_option_value.option_id
WHERE eav_attribute_option_value.value IS NOT NULL
GROUP BY eav_attribute_option_value.value, customer_group_code
OTHER TIPS
Can you try this Query
SELECT
eav_attribute_option_value.value AS 'Customer gender',
customer_group.customer_group_code AS 'Group',
CONCAT('$',
FORMAT(SUM(sales_order.`grand_total`),
2)) AS 'Grand total'
FROM
`customer_entity`
LEFT JOIN
sales_order ON customer_entity.entity_id = sales_order.customer_id
LEFT JOIN
customer_group ON customer_entity.group_id = customer_group.customer_group_id
LEFT JOIN
eav_attribute_option_value ON customer_entity.gender = eav_attribute_option_value.option_id
GROUP BY customer_entity.entity_id
ORDER BY SUM(sales_order.`grand_total`) ASC
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange