Magento直接SQL查询基于优惠券的订单?

时间:2014-10-19 20:51:11

标签: mysql magento

我正在尝试为Magento编写一个SQL查询,它将显示已支付的订单(非零总计)和基于优惠券的订单(总计为零)。基于优惠券的订单应除以优惠券所来自的促销。然后报告的每一行显示以下内容:

  • 促销名称(购物车价格规则名称)
  • 订单数量
  • 总订单百分比
  • 平均订单总数

我意识到这可能是一个非常复杂的查询,所以如果有人指出我需要执行此操作所需的表格,我们会很高兴。欢迎任何所有帮助,并感谢=)

2 个答案:

答案 0 :(得分:1)

这似乎已经成功了

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;

答案 1 :(得分:0)

查询已针对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;