-- Club subscriptions live at each month's end -- Paid-for (not gift) subscriptions begun before the month's end and not ended by then. WITH m AS (SELECT strftime(range, '%Y-%m') AS month, range AS t0, range + INTERVAL 1 MONTH AS t FROM range(TIMESTAMP '2023-10-01', TIMESTAMP '2026-10-01', INTERVAL 1 MONTH)) SELECT m.month, count(*) AS value FROM m JOIN billing.subscription x ON x.start_date < m.t AND (x.ended_at IS NULL OR x.ended_at >= m.t) AND (NOT x.is_gift) GROUP BY 1 ORDER BY 1