如何通过特定日期的唯一客户和重复客户获取每天的客户数量?
Posted
技术标签:
【中文标题】如何通过特定日期的唯一客户和重复客户获取每天的客户数量?【英文标题】:How can i get count of customers per day by unique and repeat customer for specific date? 【发布时间】:2014-09-29 05:50:39 【问题描述】:我正在尝试从我的订单表中获取结果,以获取第一次订购和重复订单的客户计数列表。如下所示。
日期 第一次时间 重复订单 2014-09-01 43 90 2014-09-02 3 45 2014-09-03 12 30 2014-09-04 32 0 2014-09-05 1 98我是 sql 初学者,我会使用 mysql。
我的表结构是这样的。
订单号 int 订单日期日期时间 CustomerID int
我已经在 mysql 中尝试过这个查询,但它只给了我第一次计时的有序计数。 选择日期(订单日期),计数(*) FROM 订单 T JOIN ( 选择 MIN(OrderDate) 作为 minDate, CustomerID 来自订单 GROUP BY CustomerID) T2 ON T.OrderDate = T2.minDate And T.CustomerID = T2.CustomerID 按日期分组(T.OrderDate)
【问题讨论】:
1st time
和repeat
的逻辑是什么?
基本上当客户 1 于 2014 年 9 月 1 日第一次下订单时。那么对于那个日期,第一次计数将增加。并且在同一客户将在 2014-09-02 下订单之后,重复订单数量将增加。
第一次和重复的订单是基于customerid的。
如果 customer1 在 2014-09-01 订购了 2 次怎么办?那么计数将如何增加?
是的,那么对于 2014-09-01 日期,两个计数都将为 1。
【参考方案1】:
您可以通过OrderDate分组获得每天的订单总数:
SELECT OrderDate, COUNT(OrderNumber) AS total FROM orders GROUP BY OrderDate
你可以拒绝。每天的第一笔订单来自以下查询:
SELECT OrderDate, COUNT(q1.CustomerID) AS first FROM (SELECT CustomerID, min(OrderDate) AS OrderDate FROM orders GROUP BY CustomerID)q1 GROUP BY q1.OrderDate
现在在 OrderDate 上加入这两个以获取第一个和重复订单的分布:
SELECT a.OrderDate, a.first, (b.total - a.first) AS repeated FROM
(SELECT OrderDate, COUNT(q1.CustomerID) AS first FROM (SELECT CustomerID, min(OrderDate) AS OrderDate FROM orders GROUP BY CustomerID)q1 GROUP BY q1.OrderDate)a
JOIN
(SELECT OrderDate, COUNT(OrderNumber) AS total FROM orders GROUP BY OrderDate)b
on(a.OrderDate = b.OrderDate)
【讨论】:
如果某一天没有新客户的订单怎么办?您如何不将其从结果中排除?【参考方案2】:一个稍微复杂的查询,但应该这样做:
首次用户:只需按 customerID 分组以获取最小订购日期,然后按该日期分组以获取特定日期的新用户数。查询如下所示:
select date(mdate) as day, COUNT(*) from (select customerid, min(orderdate) as mDate from orders GROUP BY CustomerID)q1 GROUP BY day;
重复用户:首先过滤掉所有作为第一订单放置的订单号,然后按订单日期进行分组以获得重复。查询将是:
select date(orderdate) day, COUNT(*) from (select * from orders where orderno not in (select orders.orderno from orders JOIN (select customerid, min(orderdate) as mdate from orders GROUP BY CustomerID)as order2 ON (orders.customerid = order2.customerid) and (orders.orderdate = order2.mdate))) as q1 GROUP BY day;
您可以在当天对这两个查询进行连接,以按照您提到的方式获得组合结果。如果不起作用,请告诉我
编辑: 这将是完整的查询:这里我正在对左右外连接进行 UNION,因为您可能会遇到没有新请求或没有重复请求的地方。这将处理这两种情况。
select q2.*, q3.repeated from (select date(mdate) as day, COUNT(*) as first from (select customerid, min(orderdate) as mDate from orders GROUP BY CustomerID)q1 GROUP BY day) as q2 LEFT OUTER JOIN (select date(orderdate) day, COUNT(*) as repeated from (select * from orders where orderno not in (select orders.orderno from orders JOIN (select customerid, min(orderdate) as mdate from orders GROUP BY CustomerID)as order2 ON (orders.customerid = order2.customerid) and (orders.orderdate = order2.mdate))) as q1 GROUP BY day) as q3 on q2.day = q3.day UNION select q2.*, q3.repeated from (select date(mdate) as day, COUNT(*) as first from (select customerid, min(orderdate) as mDate from orders GROUP BY CustomerID)q1 GROUP BY day) as q2 RIGHT OUTER JOIN (select date(orderdate) day, COUNT(*) as repeated from (select * from orders where orderno not in (select orders.orderno from orders JOIN (select customerid, min(orderdate) as mdate from orders GROUP BY CustomerID)as order2 ON (orders.customerid = order2.customerid) and (orders.orderdate = order2.mdate))) as q1 GROUP BY day) as q3 on q2.day = q3.day
【讨论】:
【参考方案3】:这是我的回答,但不确定是否还能改进。
SELECT userID, COUNT(*) AS repeat_order_cnt FROM
(SELECT DATE(OrderDate) AS order_DT, userID, COUNT(*) AS no_of_order FROM order
AND YEAR(orderDate) = '2015'
AND MONTH(orderDate) = '01'
GROUP BY order_DT,userID) AS order2
GROUP BY userID
HAVING COUNT(*) > 1
【讨论】:
以上是关于如何通过特定日期的唯一客户和重复客户获取每天的客户数量?的主要内容,如果未能解决你的问题,请参考以下文章