Skip to content

Instantly share code, notes, and snippets.

@jthandy
Created April 6, 2016 15:39
Show Gist options
  • Save jthandy/5e1913fa12d351b42f3310f91c9db6cc to your computer and use it in GitHub Desktop.
Save jthandy/5e1913fa12d351b42f3310f91c9db6cc to your computer and use it in GitHub Desktop.
customers as (
select customer, min(period_start) as active_from, max(period_end) as active_to
from invoices
where period_start <= current_date
group by customer
), customer_dates as (
select m.date_month, c.customer
from months m
inner join customers c
on m.date_month >= date_trunc('month', c.active_from)
and m.date_month < date_trunc('month', c.active_to)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment