如何计算postgresql中每月重复事件的差距

时间:2018-01-29 20:58:18

标签: sql postgresql gaps-and-islands

使用postgresql 9.6

问题如下,我们有一个跟踪订阅业务订单的数据库。一旦某人订阅,就会每月为他们生成订单。每个人都有能力跳过一个月(或跳过一个月的周期,或3等,有效地跳过x个月)。我试图确定有"跳过的人数"在一个月内。

我们有一个看起来像这样的订单表(简化):

CREATE TABLE orders (
  person_id varchar,
  timestamp_ timestamp
);

我可以使用窗口函数对每个人的订单进行排序,如下所示:

    select timestamp_, person_id, row_number() 
           over (partition by person_id order by timestamp_)
    from orders

输出:

 timestamp_             person_id                      row_number
2017-03-14 12:38:38 00050c43-08c5-11e7-b433-01007e15dd78    1
2017-04-14 10:04:13 00050c43-08c5-11e7-b433-01007e15dd78    2
2017-07-14 10:05:17 00050c43-08c5-11e7-b433-01007e15dd78    3
2017-08-14 10:02:37 00050c43-08c5-11e7-b433-01007e15dd78    4
2017-09-14 10:04:37 00050c43-08c5-11e7-b433-01007e15dd78    5
2017-10-14 10:02:08 00050c43-08c5-11e7-b433-01007e15dd78    6
2017-11-14 10:05:35 00050c43-08c5-11e7-b433-01007e15dd78    7
2017-12-14 10:02:52 00050c43-08c5-11e7-b433-01007e15dd78    8
2018-01-14 10:05:38 00050c43-08c5-11e7-b433-01007e15dd78    9
2017-11-15 03:54:57 000b5c80-c9b8-11e7-a1c1-0242ac110003    1
2017-12-14 10:00:34 000b5c80-c9b8-11e7-a1c1-0242ac110003    2
2018-01-14 10:07:17 000b5c80-c9b8-11e7-a1c1-0242ac110003    3
2016-12-24 10:15:58 0017c8ad-b252-11e6-b4db-0100ab184d8f    1
2017-01-24 10:54:49 0017c8ad-b252-11e6-b4db-0100ab184d8f    2

我也一直在玩这样的滞后函数:

    select timestamp_, person_id,
      (date_trunc('month', timestamp_) - date_trunc('month',timestamp_)) 
      over (partition by person_id order by timestamp_))
    from orders;

给我一​​个这样的结果:

timestamp_              person_id                           lag
2017-03-14 12:38:38 00050c43-08c5-11e7-b433-01007e15dd78    
2017-04-14 10:04:13 00050c43-08c5-11e7-b433-01007e15dd78    0 years 0 mons 31 days 0 hours 0 mins 0.00 secs
2017-07-14 10:05:17 00050c43-08c5-11e7-b433-01007e15dd78    0 years 0 mons 91 days 0 hours 0 mins 0.00 secs
2017-08-14 10:02:37 00050c43-08c5-11e7-b433-01007e15dd78    0 years 0 mons 31 days 0 hours 0 mins 0.00 secs
2017-09-14 10:04:37 00050c43-08c5-11e7-b433-01007e15dd78    0 years 0 mons 31 days 0 hours 0 mins 0.00 secs
2017-10-14 10:02:08 00050c43-08c5-11e7-b433-01007e15dd78    0 years 0 mons 30 days 0 hours 0 mins 0.00 secs
2017-11-14 10:05:35 00050c43-08c5-11e7-b433-01007e15dd78    0 years 0 mons 31 days 0 hours 0 mins 0.00 secs
2017-12-14 10:02:52 00050c43-08c5-11e7-b433-01007e15dd78    0 years 0 mons 30 days 0 hours 0 mins 0.00 secs
2018-01-14 10:05:38 00050c43-08c5-11e7-b433-01007e15dd78    0 years 0 mons 31 days 0 hours 0 mins 0.00 secs
2017-11-15 03:54:57 000b5c80-c9b8-11e7-a1c1-0242ac110003    
2017-12-14 10:00:34 000b5c80-c9b8-11e7-a1c1-0242ac110003    0 years 0 mons 30 days 0 hours 0 mins 0.00 secs
2018-01-14 10:07:17 000b5c80-c9b8-11e7-a1c1-0242ac110003    0 years 0 mons 31 days 0 hours 0 mins 0.00 secs
2016-12-24 10:15:58 0017c8ad-b252-11e6-b4db-0100ab184d8f    
2017-01-24 10:54:49 0017c8ad-b252-11e6-b4db-0100ab184d8f    0 years 0 mons 31 days 0 hours 0 mins 0.00 secs

我需要帮助将两个查询结合起来并应用group by month来计算当月跳过的人数:

select month, count(person_id) as skips
from ( some inner query)
group by month

得到这样的东西:

Month       Number of people who skipped subscription 
2017-03-1    14
2017-04-1    8
2017-05-1    4

1 个答案:

答案 0 :(得分:2)

我想到的方法是在第一个时间戳和最后一个时间戳之间为每个人生成一行(您可能希望使用固定日期)。

然后,检查哪些订单有:

select m.mon, count(*) as num_missing
from generate_series('2017-03-01'::timestamp, '2017-05-01'::timestamp, interval '1 month') m(mon) join
     (select person_id, min(timestamp_) as mints, max(timestamp_) as maxts
      from orders
      group by person_id
     ) p
     on m.mon between date_trunc('month', mints) and date_trunc('month', maxtx) left join
     orders o
     on p.person_id = o.personid and m.mon = date_trunc('month', o.timestamp_)
where o.person_id is null
group by m.mon
order by m.mon;