我正在尝试从订单表中获取结果,以获取第一次订购和重复订单的客户数量列表。像下面的东西。
Date 1st time time repeat order 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。
我的表格结构如下。
OrderNumber int OrderDate datetime CustomerID int
<小时/> 我在mysql中尝试了这个查询,但它只给了我第一次定时计数。
SELECT DATE(OrderDate), COUNT(*) FROM orders T JOIN ( SELECT MIN(OrderDate) as minDate, CustomerID FROM orders GROUP BY CustomerID) T2 ON T.OrderDate = T2.minDate AnD T.CustomerID = T2.CustomerID GROUP BY DATE(T.OrderDate)
答案 0 :(得分:0)
稍微复杂的查询,但这应该:
首次使用者:只需按customerID分组即可获取最小订单日期,然后在该日期分组,以获取特定日期的新用户数量。查询将如下所示:
select date(mdate) as day, COUNT(*) from (select customerid, min(orderdate) as mDate from orders GROUP BY CustomerID)q1 GROUP BY day;
重复用户:首先过滤掉作为第一个订单放置的所有这样的orderno,然后按orderdate进行一组重复。查询将是:
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
答案 1 :(得分:0)
您可以通过对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 :(得分:0)
这是我的答案,但不确定是否还可以改进。
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