SQL prestashop统计信息按客户组排序

时间:2012-04-12 17:48:21

标签: sql prestashop

我尝试在prestashop

中按客户群导出我的订单总额

这是我对N°3组的请求,例如:

SELECT ROUND(SUM(IFNULL(o.`total_paid_real`, 0 ) / cu.conversion_rate), 2) as totalMoneySpent
FROM `ps_orders` o
LEFT JOIN `ps_currency` cu ON o.id_currency = cu.id_currency
LEFT JOIN `ps_customer` c ON c.id_default_group= 3
WHERE o.valid = 1;

其他版本(相同结果,所有组的总数)

SELECT ROUND( SUM( IFNULL(o.`total_paid_real`, 0 ) / cu.conversion_rate), 2 ) as totalMoneySpent
FROM `ps_orders` o
LEFT JOIN `ps_currency` cu ON o.id_currency = cu.id_currency
#LEFT JOIN `ps_customer` c ON c.id_default_group=3
WHERE o.valid = 1 
AND o.id_customer IN( SELECT c.id_customer FROM `ps_customer` c  WHERE c.id_default_group=3 )
 ;

我的问题是c.id_default_group永远不会改变结果,

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

SELECT c.group_id, ROUND(SUM(IFNULL(o.`total_paid_real`, 0 ) / cu.conversion_rate), 2) as totalMoneySpent
FROM `ps_orders` o
LEFT JOIN `ps_currency` cu ON o.id_currency = cu.id_currency

答案 1 :(得分:0)

应用聚合函数SUM后,您必须GROUP BY才能获得多行的聚合结果(否则聚合将单独应用于每一行,从而无法实现其目的)

SELECT c.group_id, ROUND(SUM(IFNULL(o.`total_paid_real`, 0 ) / cu.conversion_rate), 2) as totalMoneySpent
FROM `ps_orders` o
LEFT JOIN `ps_currency` cu ON o.id_currency = cu.id_currency
LEFT JOIN `ps_customer` c ON c.id_default_group= 3
WHERE o.valid = 1
GROUP BY c.group
ORDER BY c.group

假设group_id标识您在问题中引用的客户群。