data-dwh-dbt-project/models/intermediate/cross/int_monthly_onboarding_mrr_per_deal.sql

73 lines
2.9 KiB
MySQL
Raw Normal View History

2025-01-29 10:05:41 +01:00
with
int_monthly_aggregated_metrics_history_by_deal as (
2025-01-30 16:18:29 +01:00
select
(date_trunc('month', date) + interval '2 month' - interval '1 day')::date
2025-01-30 16:30:03 +01:00
as next_month_end_date,
*
2025-01-30 16:18:29 +01:00
from {{ ref("int_monthly_aggregated_metrics_history_by_deal") }}
2025-01-29 10:05:41 +01:00
),
2025-01-29 15:44:07 +01:00
int_kpis__dimension_deals as (select * from {{ ref("int_kpis__dimension_deals") }}),
2025-01-29 10:05:41 +01:00
deal_attributes as (
select
id_deal,
coalesce(
main_billing_country_iso_3_per_deal, 'UNSET'
) as main_billing_country_iso_3_per_deal,
effective_deal_start_month,
hubspot_deal_cancellation_month,
coalesce(
hubspot_listing_segmentation, 'UNSET'
) as hubspot_listing_segmentation
2025-01-29 15:44:07 +01:00
from int_kpis__dimension_deals
2025-01-29 10:05:41 +01:00
-- Exclude deals without live dates
where effective_deal_start_date_utc is not null
)
-- Calculate expected MRR per deal by each dimension
select
2025-01-30 16:18:29 +01:00
m.next_month_end_date as date,
2025-01-29 10:05:41 +01:00
'by_number_of_listings' as dimension,
d.hubspot_listing_segmentation as dimension_value,
sum(coalesce(m.total_revenue_in_gbp, 0)) / count(*) as expected_mrr_per_deal
from int_monthly_aggregated_metrics_history_by_deal m
inner join
deal_attributes d
on m.id_deal = d.id_deal
and date_trunc('month', m.date) >= date_trunc('month', d.effective_deal_start_month)
and date_trunc('month', m.date)
<= coalesce(d.hubspot_deal_cancellation_month, '2099-01-01')
and date_trunc('month', m.date)::date <> date_trunc('month', now())::date
where d.hubspot_listing_segmentation <> 'UNSET'
group by 1, 2, 3
union all
select
2025-01-30 16:18:29 +01:00
m.next_month_end_date as date,
2025-01-29 10:05:41 +01:00
'by_billing_country' as dimension,
d.main_billing_country_iso_3_per_deal as dimension_value,
sum(coalesce(m.total_revenue_in_gbp, 0)) / count(*) as expected_mrr_per_deal
from int_monthly_aggregated_metrics_history_by_deal m
inner join
deal_attributes d
on m.id_deal = d.id_deal
and date_trunc('month', m.date) >= date_trunc('month', d.effective_deal_start_month)
and date_trunc('month', m.date)
<= coalesce(d.hubspot_deal_cancellation_month, '2099-01-01')
and date_trunc('month', m.date)::date <> date_trunc('month', now())::date
where d.hubspot_listing_segmentation <> 'UNSET'
group by 1, 2, 3
union all
select
2025-01-30 16:18:29 +01:00
m.next_month_end_date as date,
2025-01-29 10:05:41 +01:00
'global' as dimension,
'global' as dimension_value,
sum(coalesce(m.total_revenue_in_gbp, 0)) / count(*) as expected_mrr_per_deal
from int_monthly_aggregated_metrics_history_by_deal m
inner join
deal_attributes d
on m.id_deal = d.id_deal
and date_trunc('month', m.date) >= date_trunc('month', d.effective_deal_start_month)
and date_trunc('month', m.date)
<= coalesce(d.hubspot_deal_cancellation_month, '2099-01-01')
and date_trunc('month', m.date)::date <> date_trunc('month', now())::date
where d.hubspot_listing_segmentation <> 'UNSET'
group by 1, 2, 3