SELECT timestamp, UserID, URL,
SUM(newSession) -- cumulative sum over 0/1
OVER (PARTITION BY UserId
ORDER BY timestamp
ROWS UNBOUNDED PRECEDING) AS SessionID
FROM
(
SELECT
ts_col, UserID, URL,
-- calculate the timestamp difference between current and previous row
CASE WHEN timestamp - LAG(timestamp)
OVER (PARTITION BY UserId
ORDER BY timestamp) > INTERVAL 'X minutes'
THEN 1 -- new session starts
ELSE 0 -- part of the old session
END AS newSession
) AS dt
1条答案
按热度按时间jv2fixgn1#
这有点棘手,但可以使用嵌套的窗口聚合函数(如
一些dbmse(例如vertica和aster)支持使用内置函数进行会话,而在其他dbmse中,您可以实现用户定义的函数。