1WITH crm_customers AS (
2 SELECT account_key, company_name, region_code, phone
3 FROM crm.accounts
4 WHERE is_deleted = false
5), billing_customers AS (
6 SELECT customer_id, account_name, arr_usd, billed_at
7 FROM billing.subscriptions
8 WHERE is_test_account = false
9), resolved AS (
10 SELECT
11 COALESCE(c.account_key, b.customer_id) AS customer_id,
12 COALESCE(c.company_name, b.account_name) AS customer,
13 COALESCE(g.region_name, 'Unassigned') AS region,
14 DATE_TRUNC('month', b.billed_at) AS month,
15 SUM(b.arr_usd) AS revenue
16 FROM crm_customers c
17 JOIN billing_customers b
18 ON lower(trim(c.account_key)) = lower(trim(b.customer_id))
19 LEFT JOIN geo.region_map g
20 ON c.region_code = g.raw_code
21 WHERE b.billed_at >= '2026-01-01'
22 AND b.billed_at < '2026-07-01'
23 GROUP BY 1, 2, 3, 4
24)
25SELECT * FROM resolved ORDER BY revenue DESC;▊