基于“订单”列的“回溯”窗口,并将其替换为“ Orderid”和“ User ID”

时间:2018-08-03 09:22:04

标签: sql sql-server database-design analytics

我有一个如下表

----------
user_ID Timestamp   Order_ID    Channel        
----------        
ABC 30/06/2018 10:27    grt12083589 NULL        
----------        
CDE 01/07/2018 10:33    fcd12083589 NULL         
----------        
DEF 01/07/2018 14:40    NULL    1        
----------        
LPG 01/07/2018 14:45    per1133089  NULL        
----------        
ABC 13/06/2018 11:29    NULL    2         
----------        
ABC 04/07/2018 11:57    NULL    1         
----------        
EFG 01/07/2018 12:57    gfd12083705 NULL         
----------        
EFG 28/06/2018 15:24    NULL    3         
----------        
CDE 03/07/2018 01:09    NULL    2         
----------       
ABC 04/03/2018 10:27    NULL    5         
----------        
ABC 05/02/2018 10:27    NULL    1         
----------    
EFG 23/01/2018 15:24    NULL    4 
----------
EFG 04/07/2018 12:57    NULL    2     
----------

考虑到userid和OrderID列以及时间戳,我需要下面这样的结果

我需要30天用户的所有信息基于Orderid的时间戳回溯窗口,如果超过30天,请忽略该信息,并在购买后保留该用户的所有信息,直到下一次购买

它应该看起来像下面

----------
user_ID Timestamp   Order_ID    Channel 
----------
ABC 30/06/2018 10:27    grt12083589 NULL 
----------
ABC 13/06/2018 11:29    grt12083589 2 
----------
ABC 04/07/2018 11:57    NULL    1 
----------
CDE 01/07/2018 10:33    fcd12083589 NULL 
----------
CDE 03/07/2018 01:09    NULL    2 
----------
DEF 01/07/2018 14:40    NULL    1 
----------
EFG 01/07/2018 12:57    gfd12083705 NULL 
----------
EFG 28/06/2018 15:24    gfd12083705 3
----------
EFG 04/07/2018 12:57    NULL    2 
----------
LPG 01/07/2018 14:45    per1133089  
----------

请帮助

1 个答案:

答案 0 :(得分:1)

请尝试这个。

declare @table table (
  [user_id] nvarchar(3)
  ,[timestamp] datetime
  ,order_id nvarchar(100)
  ,channel int
  )

  insert into @table values 
    ('ABC', '30-jun-2018 10:27',  'grt12083589' , NULL    ),
    ('CDE', '01-jul-2018 10:33',  'fcd12083589' , NULL    ),
    ('DEF', '01-jul-2018 14:40',  NULL        , 1            ),
    ('LPG', '01-jul-2018 14:45',  'per1133089'  , NULL    ),
    ('ABC', '13-jun-2018 11:29',  NULL          , 2      ), 
    ('ABC', '04-jul-2018 11:57',  NULL          , 1      ), 
    ('EFG', '01-jul-2018 12:57',  'gfd12083705' , NULL    ),
    ('EFG', '28-jun-2018 15:24',  NULL        , 3            ),
    ('CDE', '03-jul-2018 01:09',  NULL        , 2            ),
    ('ABC', '04-mar-2018 10:27',  NULL        , 5            ),
    ('ABC', '05-feb-2018 10:27',  NULL        , 1            ),
    ('EFG', '23-jan-2018 15:24',  NULL        , 4            ),
    ('EFG', '04-jul-2018 12:57',  NULL        , 2            )


   ;with cte as 
   (
      select  DATEADD(day,-30,max([timestamp])) as max_timestamp,[user_id] 
      from @table
      group by [user_id] 
   )

    select t1.* from @table t1
    where [timestamp]> (select max_timestamp from cte t2  where t2.[user_id]=t1.[user_id]  )
    order by [user_id] asc,order_id desc,[timestamp] desc

您将得到的结果为:

+---------+-------------------------+-------------+---------+
| user_id |        timestamp        |  order_id   | channel |
+---------+-------------------------+-------------+---------+
| ABC     | 2018-06-30 10:27:00.000 | grt12083589 | NULL    |
| ABC     | 2018-07-04 11:57:00.000 | NULL        | 1       |
| ABC     | 2018-06-13 11:29:00.000 | NULL        | 2       |
| CDE     | 2018-07-01 10:33:00.000 | fcd12083589 | NULL    |
| CDE     | 2018-07-03 01:09:00.000 | NULL        | 2       |
| DEF     | 2018-07-01 14:40:00.000 | NULL        | 1       |
| EFG     | 2018-07-01 12:57:00.000 | gfd12083705 | NULL    |
| EFG     | 2018-07-04 12:57:00.000 | NULL        | 2       |
| EFG     | 2018-06-28 15:24:00.000 | NULL        | 3       |
| LPG     | 2018-07-01 14:45:00.000 | per1133089  | NULL    |
+---------+-------------------------+-------------+---------+

DEMO