Skip to content

Instantly share code, notes, and snippets.

@daniel-sim
Created January 2, 2018 14:24
Show Gist options
  • Save daniel-sim/f0a960216ba87d50795d44462e897e2d to your computer and use it in GitHub Desktop.
Save daniel-sim/f0a960216ba87d50795d44462e897e2d to your computer and use it in GitHub Desktop.
-- churned subscribers by month (only if they've paid once)
select thismonth.first_date as from_date,
thismonth.first_date + INTERVAL '1 month' as to_date,
count(*) as churned
from month thismonth
join app_history cancelled
on cancelled.event = 'Recurring charge cancelled'
and cancelled.date >= thismonth.first_date
and cancelled.date < thismonth.first_date + INTERVAL '1 month'
where exists (select 1 from app_history activated
where activated.event = 'Recurring charge activated'
and activated.shop_domain = cancelled.shop_domain
and coalesce(activated.billing_on,activated.date) <= cancelled.date
)
group by thismonth.first_date, thismonth.first_date + INTERVAL '1 month'
order by thismonth.first_date
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment