基于订单频率的SQL客户订单统计信息

时间:2018-04-30 15:36:11

标签: mysql sql

我在使用此MySQL查询时遇到问题,无法根据订单频率提取一些客户统计信息。

我已获得以下标准:

  • 在过去12个月内订购。
  • 订购三次或更多次。
  • 订单间隔不到2个月

我可以做的前两项没问题,它是最后一项。它还需要考虑以下因素:

  • 客户可能是新的,仅在6个月前首次购买(例如),并且需要从该点开始计算。

这是我到目前为止所做的:

SELECT c.customers_id, c.customers_firstname, c.customers_email_address, 
    b.biggestOrder AS largest_single_order_in_range,
    b.orders_id AS largest_single_order_id,
    COUNT(o.orders_id) AS order_count_total,
    MAX(o.date_purchased) AS last_order_date,
    MIN(o.date_purchased) AS first_order_date
FROM orders o
LEFT JOIN orders_total ot ON ot.orders_id = o.orders_id
LEFT JOIN customers c ON c.customers_id = o.customers_id
LEFT JOIN (
    SELECT o2.customers_id, o2.orders_id,
        MAX(ot2.`value`) AS biggestOrder
    FROM orders_total ot2
    LEFT JOIN orders o2 ON o2.orders_id = ot2.orders_id
    WHERE
        ot2.class = 'ot_subtotal' AND o2.orders_status = 3
    GROUP BY
        o2.customers_id
) b ON b.customers_id = c.customers_id
WHERE
    ot.class = 'ot_subtotal' AND o.orders_status = 3 
GROUP BY
    b.customers_id
HAVING
    last_order_date >= DATE_SUB(NOW(), INTERVAL 12 MONTH) 
ORDER BY
    last_order_date DESC

- 添加了表CREATE&#(示例数据/ INSERTS):

CREATE TABLE `orders` (
  `orders_id` int(11) NOT NULL AUTO_INCREMENT,
  `customers_id` int(11) NOT NULL,
  `last_modified` datetime DEFAULT NULL,
  `date_purchased` datetime DEFAULT NULL,
  `orders_status` int(5) NOT NULL,
  PRIMARY KEY (`orders_id`),
  KEY `idx_orders_customers_id` (`customers_id`),
  KEY `idx_orders_status` (`orders_status`) USING BTREE
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `orders_total` (
  `orders_total_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `orders_id` int(11) NOT NULL,
  `value` decimal(15,4) NOT NULL,
  `class` varchar(32) CHARACTER SET latin1 NOT NULL,
  PRIMARY KEY (`orders_total_id`),
  KEY `idx_orders_total_orders_id` (`orders_id`),
  KEY `idx_class` (`class`) USING BTREE
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE `customers` (
  `customers_id` int(11) NOT NULL AUTO_INCREMENT,
  `customers_firstname` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `customers_email_address` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`customers_id`),
  KEY `idx_customers_email_address` (`customers_email_address`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

更新: 这个问题很接近但不完全正确(下面的代码段不完整,但会在顶部添加到查询中)。

SELECT ...., COUNT(DISTINCT MONTH(o.date_purchased)) AS month_count
....
HAVING month_count >= 6

但这只能确保至少1个订单已放置在1个月x 6最小值,这可能会给出前6个月订单的结果,然后在剩下的6个月内没有任何订单,也会排除第一个订单订单不到6个月前。

1 个答案:

答案 0 :(得分:0)

对于您现有的查询,请添加

join (
subquery to identify orders within 3 months of each other
) frequentOrders on frequentOrders.customerId = o.customerID

该子查询将遵循此逻辑

from orders FirstOrder join Orders NextOrder on FirstOrder.CustomerID = NextOrder.CustomerId

where NextOrder.Date_purchased > FirstOrder.DatePurchased
and they are within 3 months of each other
and they are both within the date range of the search

我不使用MySQL,所以我不会假装知道语法。