在sql中,在12个月的时间内将重复客户分成几次?

时间:2017-12-07 08:07:14

标签: sql sql-server

我一直试图获得第一次购买,第二次和第三次超过12个月购买的客户数量。像这样: enter image description here

我找到了一个类似的脚本,显示了重复客户,但在几个月或几个月内没有分裂。 How can i get count of customers per day by unique and repeat customer for specific date?

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) 

以下是示例数据:

enter image description here

我是sql的初学者,并且已经尝试修改了几天。

谢谢!

0 个答案:

没有答案