尝试在 clickhouse 中记录连续的时间戳条纹
Trying to record consecutive timestamp streaks within clickhouse
所以,我有一个 table 存储用户 ID 和会话时间。我想获得每个用户的最大连胜记录。裸奔每周至少 1 次,持续 (streak_size) 周
我的table是这样的:
create table sessions(userid String, session_ts DateTime)
Engine = MergeTree Order By(session_ts);
insert into sessions values
('user1','2020-12-13 00:00:00'),('user1','2020-12-20 00:00:00'),
('user1','2020-12-27 00:00:00'),('user1','2021-01-10 00:00:00'),
('user1','2021-01-17 00:00:00');
并将一行与下一行进行比较,以查看用户 ID 是否相等以及周是否正好相隔 1 周
问题是我不知道如何让它停止计数,或者在连胜被打破时开始新的计数
我希望我的输入看起来像:
[userid: user1, streak: 3]
这是一个任务“gaps-and-islands”
SELECT
userid,
arrayMap(i -> length(i), arraySplit((x, y) -> (y > 1), arrayDifference(groupArray(weeknum)) AS x, x)) AS r,
arrayMax(r) AS r1
FROM
(
SELECT userid, toRelativeWeekNum(session_ts) weeknum FROM sessions ORDER BY userid, weeknum
)
GROUP BY userid
┌─userid─┬─r─────┬─r1─┐
│ user1 │ [3,2] │ 3 │
└────────┴───────┴────┘
所以,我有一个 table 存储用户 ID 和会话时间。我想获得每个用户的最大连胜记录。裸奔每周至少 1 次,持续 (streak_size) 周
我的table是这样的:
create table sessions(userid String, session_ts DateTime)
Engine = MergeTree Order By(session_ts);
insert into sessions values
('user1','2020-12-13 00:00:00'),('user1','2020-12-20 00:00:00'),
('user1','2020-12-27 00:00:00'),('user1','2021-01-10 00:00:00'),
('user1','2021-01-17 00:00:00');
并将一行与下一行进行比较,以查看用户 ID 是否相等以及周是否正好相隔 1 周
问题是我不知道如何让它停止计数,或者在连胜被打破时开始新的计数
我希望我的输入看起来像:
[userid: user1, streak: 3]
这是一个任务“gaps-and-islands”
SELECT
userid,
arrayMap(i -> length(i), arraySplit((x, y) -> (y > 1), arrayDifference(groupArray(weeknum)) AS x, x)) AS r,
arrayMax(r) AS r1
FROM
(
SELECT userid, toRelativeWeekNum(session_ts) weeknum FROM sessions ORDER BY userid, weeknum
)
GROUP BY userid
┌─userid─┬─r─────┬─r1─┐
│ user1 │ [3,2] │ 3 │
└────────┴───────┴────┘