确定客户订单之间的差距

时间:2016-10-13 20:04:04

标签: sql sql-server

我被困住了。我需要进入客户订单数据库,并确定客户在13个月或更长时间不活动后下订单。这将是7年内约30,000名客户和约355,000份订单。

例如......假设客户#123在接下来的几个月内下了订单:

CustomerNumber  OrderMonth
123             Jan 2010
123             Feb 2010
123             Apr 2010
123             Jul 2011
123             Jan 2013
123             Feb 2013
123             Aug 2015

在这个例子中,我需要返回三行,其中包含客户123的以下几个月,因为每个行都有至少13个月没有订单的差距。

CustomerNumber  OrderMonth
123             Jul 2011
123             Jan 2013
123             Aug 2015

我在SQL Server 2008 R2上,因此工具栏中没有Lead / Lag。我通常可以发明某种解决方案,即使不是最实用/最有效的......但是这个让我感到难过。任何想法将不胜感激!

4 个答案:

答案 0 :(得分:2)

df <- read.csv('twit_data.csv', sep=';')

返回

Declare @YourTable table (CustomerNumber int, OrderMonth Date)
Insert Into @YourTable values
(123,'2010-01-01'),
(123,'2010-02-01'),
(123,'2010-04-01'),
(123,'2011-07-01'),
(123,'2013-01-01'),
(123,'2013-02-01'),
(123,'2015-08-01')

;with cteBase as (
    Select * 
          ,RowNr = Row_Number() over (Partition By CustomerNumber Order By OrderMonth)
     From @YourTable
)
Select A.CustomerNumber
      ,OrderMonth     = Left(DateName(MM,A.OrderMonth),3)+' '+cast(Year(A.OrderMonth) as varchar(4))
      ,NumberOfMonths = DateDiff(MM,B.OrderMonth,A.OrderMonth)
  From cteBase A
  Join cteBase B on (A.RowNr=B.RowNr+1)
  Where DateDiff(MM,B.OrderMonth,A.OrderMonth)>=13

答案 1 :(得分:1)

找出如何将表与自身连接的方法,但每个订单都与之前的订单相匹配。你没有在那里显示所有cols,但想象你有一个名为OrderNumber的字段(可以计算)。然后你会写出类似的东西:

SELECT ...
FROM Orders a JOIN Orders b ON a.CutomerId = b.CustomerId AND a.OrderNumber = b.OrderNumber + 1
WHERE DateDiff(a.OrderDate, b.OrderDate, M) > 13

答案 2 :(得分:0)

您始终可以使用通用ModelState

not exists

答案 3 :(得分:0)

您可以使用线索来获得差异(对于SQL Server&gt; 2012)

;WITH cte
AS (SELECT
    *, LEAD(ordermonth, 1, ordermonth) OVER (PARTITION BY customerNumber ORDER BY ordermonth) AS nextordermonth
FROM yourtable)
SELECT CustomerNumber, nextordermonth
FROM cte
WHERE DATEDIFF(m, ordermonth, nextordermonth) > 13

表格结构

create table yourtable (CustomerNumber Int, OrderMonth date)

Insert Into YourTable values
(123,'2010-01-01'),
(123,'2010-02-01'),
(123,'2010-04-01'),
(123,'2011-07-01'),
(123,'2013-01-01'),
(123,'2013-02-01'),
(123,'2015-08-01')