简体   繁体   English

Postgres累积计数随着时间的推移

[英]Postgres cumulative count over time

I'm trying to find the number of active subscriptions in a month over a timescale. 我试图在一个月内找到一个月内活跃订阅的数量。

Table: Subscriptions Fields: 表:订阅字段:

  • id ID
  • "creationDate" “创立日期”
  • "deletionDate" “deletionDate”

A subscription is considered active at a particular timestamp when: 在以下情况下,订阅在特定时间戳中被视为活动:

  1. deletionDate is null deletionDate为null
  2. the particular timestamp falls between creationDate and deletionDate 特定时间戳位于creationDate和deletionDate之间

Example: 例:

  • Subscription A has creationDate "2014-06-27 11:37:34.205+00" and deletionDate "2014-08-01 04:16:34.435+00". 订阅A具有creationDate“2014-06-27 11:37:34.205 + 00”和deletionDate“2014-08-01 04:16:34.435 + 00”。 Subscription A is considered active in June 2014, July 2014, and Aug 2014. 订阅A在2014年6月,2014年7月和2014年8月被视为有效。
  • Subscriptions B has creationDate "2014-06-27 11:37:34.205+00" and deletionDate "2014-06-28 11:37:34.205+00". 订阅B有creationDate“2014-06-27 11:37:34.205 + 00”和deletionDate“2014-06-28 11:37:34.205 + 00”。 Subscription B is only active in June 2014. 订阅B仅在2014年6月有效。
  • Subscription C has creationDate "2014-06-27 11:37:34.205+00" and no deletionDate. 订阅C有creationDate“2014-06-27 11:37:34.205 + 00”,没有删除日期。 Subscription C is considered active for months June 2014 onwards till the current month. 认购C在2014年6月之后被认为有效,直至当月。

This is what I tried: 这是我试过的:

select "Month", sum(sub) over (order by "Month" asc) as "Active subscriptions"
from
(select to_char(subscriptions."creationDate" at time zone '-7', 'YYYY-MM') as "Month", 
    count(distinct subscriptions.id) as sub
    from subscriptions
    where (to_char(subscriptions."deletionDate" at time zone '-7', 'YYYY-MM') is null 
        or to_char(subscriptions."deletionDate" at time zone '-7', 'YYYY-MM') >= to_char(subscriptions."creationDate" at time zone '-7', 'YYYY-MM') )
    group by "Month") as foo

However, the issue with this is that it is including the count of non-active subscriptions in the previous month. 但是,问题在于它包含了上个月的非活动订阅数。 To illustrate what I mean, my query above seems to include Subscription B (in example above) as an active subscription in July 2014. 为了说明我的意思,我的上述查询似乎包括2014年7月的订阅B(在上面的示例中)作为有效订阅。

I'm not sure how to get my "Active subscriptions" for a particular month to remove the count of subscriptions that are no longer active in the previous months. 我不确定如何获取特定月份的“有效订阅”以删除前几个月不再有效的订阅数。

Thank you! 谢谢! :) :)

SELECT m, count(subscriptions.*) 
FROM subscriptions 
JOIN generate_series('2010-01-01'::date, now(), interval '1 mon') AS m
ON m >= subscriptions.creationDate AND 
       (subscriptions.deletionDate IS NULL OR m <= subscriptions.deletionDate)
/* You may get better indexed performance if you use a date 
   far in the future for current
  accounts, instead of NULL. Then you can use BETWEEN */
GROUP BY m ORDER BY m;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM