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