使用SQL计算最长的狂欢观察条纹

时间:2013-09-30 08:33:58

标签: sql postgresql

我正在尝试查看一些暴饮暴食统计数据,我想了解最长狂欢条纹的持续时间( binge 是多个程序被查看在让步,一个接一个,相隔不超过2小时)。数据如下所示:

datetime                user_id program
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  <-- user 2 binge start
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  <-- user 2 binge end
2013-09-21 09:03:22     2       H
2013-09-03 18:21:09     3       D
2013-09-21 09:03:22     2       H
2013-09-24 19:21:00     2       X  <-- user 2 second binge start
2013-09-24 20:21:00     2       Y
2013-09-24 21:21:00     2       Z  <-- user 2 second binge end

SQL Fiddle

在这个例子中,用户2有一个持续了6个小时的狂欢,后来又持续了2个小时。

我想要的最终结果是:

user_id     binge     length
2           1         6 hours
2           2         2 hours

可以直接在数据库中计算吗?

2 个答案:

答案 0 :(得分:3)

这是识别数据中的序列/条纹的问题。我首选的方法是,

  • 使用LAG功能识别每个条纹的开头
  • 使用SUM函数为每个条纹指定唯一编号
  • 然后按此唯一编号分组以进行进一步处理

查询:

with start_grp as (
  select dt, user_id, programme,
         case when dt - lag(dt,1) over (partition by user_id order by dt) 
                   > interval '0 day 2:00:00'
              then 1
              else 0
         end grp_start
  from binge
  ),
assign_grp as (
  select dt, user_id, programme,
  sum(grp_start) over (partition by user_id order by dt) grp
  from start_grp)
select user_id, grp as binge, max(dt) - min(dt) as binge_length
from assign_grp
group by user_id, grp
having count(programme) > 1

此处binge列可能不是按顺序方式进行的。您可以在最终查询中使用ROW_NUMBER函数来更正它。

sqlfiddle

演示

答案 1 :(得分:1)

这是一个使用recursive CTE的解决方案(它不是真正的“递归”,但这就是它们的调用方式)和window functions。至少你需要PostgreSQL 8.4。

SQL Fiddle

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

<强> Results

| 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_iddatetime排序查看,但这也会添加一个额外的列,其中包含上一次查看的时间戳,以便您以后可以将它们链接起来。这不是递归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

<强> Results

| 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可能有点难以理解。 “递归”依赖于两个select语句之间的联合。

  • 第一个种子迭代(它是非递归部分):它将找到作为一系列视图开始的行(即,如果前一个日期时间是该用户的第一个,或者上一个日期时间超过了您的截止时间间隔。)
  • 第二个链条可以观看更长的时间。一些持续时间将重叠,因为它不知道它何时结束。这是使用条件(在顶部的整体查询中)查找最大值并通过单次查看消除期间的地方。

查询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

<强> Results

| 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 |