【发布时间】:2014-10-19 20:51:11
【问题描述】:
我正在尝试为 Magento 编写一个 SQL 查询,该查询将显示已支付的订单(总计非零)和基于优惠券的订单(总计零)。基于优惠券的订单应除以优惠券的促销活动。然后报告的每一行显示以下内容:
- 促销名称(购物车价格规则名称)
- 订单数量
- 占总订单的百分比
- 平均订单总数
我意识到这可能是一个非常复杂的查询,因此如果有人指出我执行此操作所需的表,我会很高兴。欢迎提供任何帮助并表示感谢 =)
【问题讨论】:
我正在尝试为 Magento 编写一个 SQL 查询,该查询将显示已支付的订单(总计非零)和基于优惠券的订单(总计零)。基于优惠券的订单应除以优惠券的促销活动。然后报告的每一行显示以下内容:
我意识到这可能是一个非常复杂的查询,因此如果有人指出我执行此操作所需的表,我会很高兴。欢迎提供任何帮助并表示感谢 =)
【问题讨论】:
这似乎成功了
SELECT coupon_rule_name AS 'Promotion Used'
, coupon_code AS 'Code Used'
, COUNT(coupon_code) AS 'Times Used / Number of Orders'
, SUM(subtotal) AS 'Cumulative Price'
, SUM(total_paid) AS 'Cumulative Paid with Coupon'
, AVG(total_paid) AS 'Average Order Total (W/ Coupon)'
, AVG(subtotal) AS 'Average Order Total (W/O Coupon)'
, ABS(SUM(discount_amount)) AS 'Cumulative Savings'
, (
SUM(discount_amount) - SUM(total_paid)
) AS 'Cumulative Loss'
, CONCAT(ROUND((
COUNT(coupon_code) / (SELECT COUNT(*) FROM sales_flat_order s)
) * 100, 1), '%') AS 'Percentage'
FROM sales_flat_order
WHERE coupon_code IS NOT NULL
GROUP BY coupon_code
ORDER BY COUNT(coupon_code) DESC;
【讨论】:
针对 Magento 2 调整的查询 所有功劳归于查询@ehime 的原始海报。 我对 sales_flat_order 表进行了小改动,改为 sales_order
SELECT coupon_rule_name AS 'Promotion Used'
, coupon_code AS 'Code Used'
, COUNT(coupon_code) AS 'Times Used / Number of Orders'
, SUM(subtotal) AS 'Cumulative Price'
, SUM(total_paid) AS 'Cumulative Paid with Coupon'
, AVG(total_paid) AS 'Average Order Total (W/ Coupon)'
, AVG(subtotal) AS 'Average Order Total (W/O Coupon)'
, ABS(SUM(discount_amount)) AS 'Cumulative Savings'
, (
SUM(discount_amount) - SUM(total_paid)
) AS 'Cumulative Loss'
, CONCAT(ROUND((
COUNT(coupon_code) / (SELECT COUNT(*) FROM sales_order s)
) * 100, 1), '%') AS 'Percentage'
FROM sales_order
WHERE coupon_code IS NOT NULL
GROUP BY coupon_code
ORDER BY COUNT(coupon_code) DESC;
【讨论】: