这是一个使用递归 CTE(它不是真正的“递归”,但这就是它们的名称)和窗口函数的解决方案。为此,您至少需要 PostgreSQL 8.4。
SQL小提琴
PostgreSQL 9.1.9 模式设置:
CREATE TABLE viewings (
user_id INTEGER NOT NULL,
datetime TIMESTAMPTZ NOT NULL,
programme TEXT NOT NULL,
PRIMARY KEY (user_id, datetime)
);
INSERT INTO viewings (datetime, user_id, programme) VALUES
('2013-09-01 00:01:18', 1, 'A'),
('2013-09-10 14:03:14', 1, 'B'),
('2013-09-20 17:02:12', 2, 'A'),
('2013-09-21 00:03:22', 2, 'C'),
('2013-09-21 01:23:22', 2, 'M'),
('2013-09-21 03:03:22', 2, 'E'),
('2013-09-21 04:03:22', 2, 'F'),
('2013-09-21 06:03:22', 2, 'G'),
('2013-09-21 09:03:22', 2, 'H'),
('2013-09-03 18:21:09', 3, 'D'),
('2013-09-22 09:03:22', 2, 'H'),
('2013-09-24 19:21:00', 2, 'X'),
('2013-09-24 20:21:00', 2, 'Y'),
('2013-09-24 21:21:00', 2, 'Z');
查询 1:
WITH RECURSIVE consecutive_viewings(user_id, first_dt, last_dt) AS (
WITH lagged_viewings AS (
SELECT user_id, LAG(user_id) OVER w AS prev_user_id,
datetime, LAG(datetime) OVER w AS prev_datetime,
programme
FROM viewings WINDOW w AS (PARTITION BY user_id ORDER BY datetime)
)
SELECT user_id, datetime AS first_dt, datetime AS last_dt
FROM lagged_viewings
WHERE prev_datetime IS NULL OR (prev_datetime + '2 hours'::interval) < datetime
UNION ALL
SELECT lv.user_id, cv.first_dt, lv.datetime AS last_dt
FROM consecutive_viewings cv
INNER JOIN lagged_viewings lv
ON lv.user_id=cv.user_id AND
lv.prev_datetime=cv.last_dt
WHERE (lv.prev_datetime + '2 hours'::interval) >= lv.datetime
)
SELECT user_id, first_dt, MAX(last_dt) AS last_dt
FROM consecutive_viewings
WHERE first_dt != last_dt
GROUP BY user_id, first_dt
ORDER BY user_id, first_dt
结果:
| USER_ID | FIRST_DT | LAST_DT |
|---------|----------------------------------|----------------------------------|
| 2 | September, 21 2013 00:03:22+0000 | September, 21 2013 06:03:22+0000 |
| 2 | September, 24 2013 19:21:00+0000 | September, 24 2013 21:21:00+0000 |
要理解这一点,从最嵌套的 CTE 开始可能更容易。这将按user_id
和对查看进行排序datetime
,但这还将添加一个带有先前查看时间戳的额外列,以便您以后可以将它们链接起来。这不是递归 CTE(以下查询甚至不需要 CTE):
查询 2:
WITH lagged_viewings AS (
SELECT user_id, LAG(user_id) OVER w AS prev_user_id,
datetime, LAG(datetime) OVER w AS prev_datetime,
programme
FROM viewings WINDOW w AS (PARTITION BY user_id ORDER BY datetime)
)
SELECT * FROM lagged_viewings
结果:
| USER_ID | PREV_USER_ID | DATETIME | PREV_DATETIME | PROGRAMME |
|---------|--------------|----------------------------------|----------------------------------|-----------|
| 1 | (null) | September, 01 2013 00:01:18+0000 | (null) | A |
| 1 | 1 | September, 10 2013 14:03:14+0000 | September, 01 2013 00:01:18+0000 | B |
| 2 | (null) | September, 20 2013 17:02:12+0000 | (null) | A |
| 2 | 2 | September, 21 2013 00:03:22+0000 | September, 20 2013 17:02:12+0000 | C |
| 2 | 2 | September, 21 2013 01:23:22+0000 | September, 21 2013 00:03:22+0000 | M |
| 2 | 2 | September, 21 2013 03:03:22+0000 | September, 21 2013 01:23:22+0000 | E |
| 2 | 2 | September, 21 2013 04:03:22+0000 | September, 21 2013 03:03:22+0000 | F |
| 2 | 2 | September, 21 2013 06:03:22+0000 | September, 21 2013 04:03:22+0000 | G |
| 2 | 2 | September, 21 2013 09:03:22+0000 | September, 21 2013 06:03:22+0000 | H |
| 2 | 2 | September, 22 2013 09:03:22+0000 | September, 21 2013 09:03:22+0000 | H |
| 2 | 2 | September, 24 2013 19:21:00+0000 | September, 22 2013 09:03:22+0000 | X |
| 2 | 2 | September, 24 2013 20:21:00+0000 | September, 24 2013 19:21:00+0000 | Y |
| 2 | 2 | September, 24 2013 21:21:00+0000 | September, 24 2013 20:21:00+0000 | Z |
| 3 | (null) | September, 03 2013 18:21:09+0000 | (null) | D |
这个递归 CTE id 可能有点难以理解。“递归”依赖于两个选择语句之间的联合。
- 第一个种子迭代(它是非递归部分):它将找到作为查看链开始的行(即,如果它是该用户的第一个日期时间,则前一个日期时间为空,或者前一个日期时间是比你的截止间隔更远)。
- 第二个链接观看时间更长。有些持续时间会重叠,因为它不知道什么时候结束。这是使用条件(在顶部的整体查询中)的地方,以查找最大值并通过单次查看消除周期。
查询 3:
WITH RECURSIVE consecutive_viewings(user_id, first_dt, last_dt) AS (
WITH lagged_viewings AS (
SELECT user_id, LAG(user_id) OVER w AS prev_user_id,
datetime, LAG(datetime) OVER w AS prev_datetime,
programme
FROM viewings WINDOW w AS (PARTITION BY user_id ORDER BY datetime)
)
-- These are the starts of the "binge" durations
SELECT user_id, datetime AS first_dt, datetime AS last_dt
FROM lagged_viewings
WHERE prev_datetime IS NULL OR (prev_datetime + '2 hours'::interval) < datetime
UNION ALL
-- These are the extended periods
SELECT lv.user_id, cv.first_dt, lv.datetime AS last_dt
FROM consecutive_viewings cv
INNER JOIN lagged_viewings lv
ON lv.user_id=cv.user_id AND
lv.prev_datetime=cv.last_dt
WHERE (lv.prev_datetime + '2 hours'::interval) >= lv.datetime
)
SELECT * FROM consecutive_viewings
ORDER BY user_id, first_dt, last_dt
结果:
| USER_ID | FIRST_DT | LAST_DT |
|---------|----------------------------------|----------------------------------|
| 1 | September, 01 2013 00:01:18+0000 | September, 01 2013 00:01:18+0000 |
| 1 | September, 10 2013 14:03:14+0000 | September, 10 2013 14:03:14+0000 |
| 2 | September, 20 2013 17:02:12+0000 | September, 20 2013 17:02:12+0000 |
| 2 | September, 21 2013 00:03:22+0000 | September, 21 2013 00:03:22+0000 |
| 2 | September, 21 2013 00:03:22+0000 | September, 21 2013 01:23:22+0000 |
| 2 | September, 21 2013 00:03:22+0000 | September, 21 2013 03:03:22+0000 |
| 2 | September, 21 2013 00:03:22+0000 | September, 21 2013 04:03:22+0000 |
| 2 | September, 21 2013 00:03:22+0000 | September, 21 2013 06:03:22+0000 |
| 2 | September, 21 2013 09:03:22+0000 | September, 21 2013 09:03:22+0000 |
| 2 | September, 22 2013 09:03:22+0000 | September, 22 2013 09:03:22+0000 |
| 2 | September, 24 2013 19:21:00+0000 | September, 24 2013 19:21:00+0000 |
| 2 | September, 24 2013 19:21:00+0000 | September, 24 2013 20:21:00+0000 |
| 2 | September, 24 2013 19:21:00+0000 | September, 24 2013 21:21:00+0000 |
| 3 | September, 03 2013 18:21:09+0000 | September, 03 2013 18:21:09+0000 |