Refactor growth score by deal to stop depending on cross models

This commit is contained in:
uri 2025-04-02 16:57:19 +02:00
parent 0fd9b2ce06
commit 80f14c7b72

View file

@ -1,28 +1,78 @@
{{ config(materialized="table", unique_key=["date", "id_deal"]) }}
with
int_monthly_aggregated_metrics_history_by_deal as (
select * from {{ ref("int_monthly_aggregated_metrics_history_by_deal") }}
),
int_kpis__dimension_deals as (select * from {{ ref("int_kpis__dimension_deals") }}),
int_hubspot__deal as (select * from {{ ref("int_hubspot__deal") }}),
int_kpis__lifecycle_daily_deal as (
select * from {{ ref("int_kpis__lifecycle_daily_deal") }}
),
int_kpis__agg_dates_main_kpis as (
select *
from {{ ref("int_kpis__agg_dates_main_kpis") }}
where
dimension in ('by_deal')
and dimension_value <> 'UNSET'
and is_end_of_month = true
),
created_bookings as (
select *
from {{ ref("int_kpis__agg_monthly_created_bookings") }}
where dimension in ('by_deal') and dimension_value <> 'UNSET'
),
listings as (
select *
from {{ ref("int_kpis__agg_daily_listings") }}
where
dimension in ('by_deal')
and dimension_value <> 'UNSET'
and is_end_of_month = true
),
total_and_retained_revenue as (
select *
from {{ ref("int_kpis__agg_monthly_total_and_retained_revenue") }}
where dimension in ('by_deal') and dimension_value <> 'UNSET'
),
deal_history_from_previous_months as (
select
year,
month,
date,
date_trunc('month', date)::date as first_day_month,
id_deal,
client_type,
main_deal_name,
has_active_pms,
active_pms_list,
main_billing_country_iso_3_per_deal,
deal_lifecycle_state,
coalesce(total_revenue_in_gbp, 0) as revenue_in_gbp,
coalesce(created_bookings, 0) as created_bookings,
coalesce(listings_booked_in_month, 0) as listings_booked_in_month
from int_monthly_aggregated_metrics_history_by_deal am
d.year,
d.month,
d.date,
date_trunc('month', d.date)::date as first_day_month,
-- DEAL STATIC ATTRIBUTES --
ikdd.id_deal,
ikdd.client_type,
ikdd.main_deal_name,
ikdd.has_active_pms,
ikdd.active_pms_list,
ikdd.main_billing_country_iso_3_per_deal,
-- DEAL LIFECYCLE --
deal_lifecycle.deal_lifecycle_state,
-- METRICS --
coalesce(
total_and_retained_revenue.total_revenue_in_gbp, 0
) as revenue_in_gbp,
coalesce(created_bookings.created_bookings, 0) as created_bookings,
coalesce(listings.listings_booked_in_month, 0) as listings_booked_in_month
from int_kpis__agg_dates_main_kpis d
left join int_kpis__dimension_deals ikdd on d.dimension_value = ikdd.id_deal
left join
int_kpis__lifecycle_daily_deal deal_lifecycle
on d.date = deal_lifecycle.date
and d.dimension_value = deal_lifecycle.id_deal
left join
created_bookings
on d.date = created_bookings.end_date
and d.dimension_value = created_bookings.dimension_value
left join
listings
on d.date = listings.date
and d.dimension_value = listings.dimension_value
left join
total_and_retained_revenue
on d.date = total_and_retained_revenue.end_date
and d.dimension_value = total_and_retained_revenue.dimension_value
-- Do not show data of ongoing month
where am.date < date_trunc('month', current_date)::date
where d.date < date_trunc('month', current_date)::date
),
revenue_12_months_window_aggregation_per_deal as (
select