2017-03-15 39 views
1

目标是通过所述记录之间的超时将关于用户操作的跨国信息分组为会话。 (例如:在1-3分钟之内在每个 - >会话#1之间执行10次相同操作;以及在两次之间再次执行10次操作并在几分钟之间 - >会话#2之间执行10次操作)对网络会话进行分组

采样输入:

id user_id trans_datetime 
1  1   2017-03-16 07:12:01 
2  2   2017-03-16 07:12:02 
3  2   2017-03-16 07:12:12 
4  1   2017-03-16 08:57:00 
5  1   2017-03-16 08:58:01 
6  1   2017-03-16 09:01:50 
7  1   2017-03-16 10:14:01 
8  1   2017-03-16 10:18:01 
9  1   2017-03-16 10:35:11 

预期输出:

id start_id user_id trans_datetime 
1 1  1   2017-03-16 07:12:01 
2 2  2   2017-03-16 07:12:02 
3 2  2   2017-03-16 07:12:12 
4 4  1   2017-03-16 08:57:00 
5 4  1   2017-03-16 08:58:01 
6 4  1   2017-03-16 09:01:50 
7 7  1   2017-03-16 10:14:01 
8 7  1   2017-03-16 10:18:01 
9 7  1   2017-03-16 10:35:11 

我最初的想法是使用递归CTE为:

With rCTE as (
Select id 
    ,id as start_id 
    ,user_id 
    ,tran_datetime 
from transactions 
where first_transaction_flg = 1 

Union all 

Select child.id 
    ,parent.id as start_id 
    ,child.user_id 
    ,child.tran_datetime 
from transactions child 
Inner Join rCTE parent 
on child.user_id = parent.user_id 
    and child.tran_datetime > parent.datetime 
    and datediff(minute, child.tran_datetime, parent.tran_datetime) < 20 
) 
Select * from rCTE 

但它似乎并没有像预期的那样工作,我无法完全理解为什么。

+2

请用一些示例数据解释,以文本形式 – TheGameiswar

+0

W添加预期结果你正在使用的帽子版本? –

+0

@ZoharPeled 2014 – JagdCrab

回答

1

使用common table expression使用子查询检查是否有每个trans_datetime有效之前的活动,以及outer apply()

;with ses as (
    select 
     t.* 
    , prevTime = (
     select max(i.trans_datetime) 
     from t as i 
     where i.user_id = t.user_id 
      and i.trans_datetime < t.trans_datetime 
      and i.trans_datetime >= dateadd(hour,-1,t.trans_datetime) 
     ) 
    from t 
) 
select 
    id 
    , start_id = case 
     when prevTime is null 
     then id 
     else x.start_id 
     end 
    , user_id 
    , trans_datetime 
from ses 
outer apply (
    select top 1 
    start_id = id 
    from ses i 
    where i.user_id = ses.user_id 
    and i.trans_datetime < ses.trans_datetime 
    and i.prevTime is null 
    order by trans_datetime desc 
    ) x 

rextester演示:http://rextester.com/CGJSX81463

回报:

+----+----------+---------+---------------------+ 
| id | start_id | user_id | trans_datetime | 
+----+----------+---------+---------------------+ 
| 1 |  1 |  1 | 2017-03-16 07:12:01 | 
| 2 |  2 |  2 | 2017-03-16 07:12:02 | 
| 3 |  2 |  2 | 2017-03-16 07:12:12 | 
| 4 |  4 |  1 | 2017-03-16 08:57:00 | 
| 5 |  4 |  1 | 2017-03-16 08:58:01 | 
| 6 |  4 |  1 | 2017-03-16 09:01:50 | 
| 7 |  7 |  1 | 2017-03-16 10:14:01 | 
| 8 |  7 |  1 | 2017-03-16 10:18:01 | 
| 9 |  7 |  1 | 2017-03-16 10:35:11 | 
+----+----------+---------+---------------------+