0

我尝试在 prestashop 中按客户组导出我的订单总额

这是我对第 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 永远不会改变结果,

谢谢你的帮助

4

2 回答 2

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
于 2016-10-31T21:09:50.433 回答
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标识您在问题中提到的客户组。

于 2012-04-12T19:48:32.093 回答