简体   繁体   中英

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
  • "creationDate"
  • "deletionDate"

A subscription is considered active at a particular timestamp when:

  1. deletionDate is null
  2. the particular timestamp falls between creationDate and deletionDate

Example:

  • Subscription A has creationDate "2014-06-27 11:37:34.205+00" and deletionDate "2014-08-01 04:16:34.435+00". Subscription A is considered active in June 2014, July 2014, and Aug 2014.
  • Subscriptions B has creationDate "2014-06-27 11:37:34.205+00" and deletionDate "2014-06-28 11:37:34.205+00". Subscription B is only active in June 2014.
  • Subscription C has creationDate "2014-06-27 11:37:34.205+00" and no deletionDate. Subscription C is considered active for months June 2014 onwards till the current month.

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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