【问题标题】:How to count gaps in in monthly recurring events in postgresql如何计算postgresql中每月重复事件的间隔
【发布时间】:2018-07-19 11:01:02
【问题描述】:

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

【问题讨论】:

  • 如果某人有从 01/2017 到 12/2017 的记录,即缺少 01/2018 的条目,我如何知道该人当时是否已结束订阅或只是跳过几个月?对于此人,我是否应将 01/2018 视为已跳过? (如果你回答:“跳过”,那么:如果上个月是 1997 年 12 月,那么这个人只是跳过了 20 年吗?;-)
  • 好问题,假设我们有一个订阅终止的事件,所以我们可以划定那些跳过

标签: sql postgresql gaps-and-islands


【解决方案1】:

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

然后,查看哪些有订单:

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;

【讨论】:

  • 在 where 子句中你有 t.person_id,看起来像一个错字,因为没有 t 别名。
  • @maxTrialfire 。 . .是的,这是一个错字。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-11
  • 1970-01-01
  • 1970-01-01
  • 2016-09-23
  • 1970-01-01
  • 2012-10-28
相关资源
最近更新 更多