Skip to content

Instantly share code, notes, and snippets.

@jthandy
Created April 6, 2016 15:54
Show Gist options
  • Save jthandy/3e43e2ecd18c24edb79da249da3201fd to your computer and use it in GitHub Desktop.
Save jthandy/3e43e2ecd18c24edb79da249da3201fd to your computer and use it in GitHub Desktop.
select date_month, d.customer, period_start, period_end,
"interval" as period,
case "interval"
when 'yearly'
then coalesce(i.total, 0)::float / 12 / 100
else
coalesce(i.total, 0)::float / 100
end as total,
case min(date_month) over(partition by d.customer)
when date_month then 1
else 0
end as first_payment,
case max(date_month) over(partition by d.customer)
when date_month then 1
else 0
end as last_payment
from customer_dates d
left outer join invoices i
on d.date_month >= date_trunc('month', i.period_start)
and d.date_month < date_trunc('month', i.period_end)
and d.customer = i.customer
left outer join {{env.schema}}.stripe_subscriptions s on i.subscription_id = s.id
left outer join {{env.schema}}.stripe_plans p on s.plan_id = p.id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment