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
但它似乎并没有像预期的那样工作,我无法完全理解为什么。
请用一些示例数据解释,以文本形式 – TheGameiswar
W添加预期结果你正在使用的帽子版本? –
@ZoharPeled 2014 – JagdCrab