From 80f14c7b72db6908db9809796747c8cf9a307b8e Mon Sep 17 00:00:00 2001 From: uri Date: Wed, 2 Apr 2025 16:57:19 +0200 Subject: [PATCH] Refactor growth score by deal to stop depending on cross models --- .../int_monthly_growth_score_by_deal.sql | 88 +++++++++++++++---- 1 file changed, 69 insertions(+), 19 deletions(-) diff --git a/models/intermediate/cross/int_monthly_growth_score_by_deal.sql b/models/intermediate/cross/int_monthly_growth_score_by_deal.sql index d5e240c..3a87dd0 100644 --- a/models/intermediate/cross/int_monthly_growth_score_by_deal.sql +++ b/models/intermediate/cross/int_monthly_growth_score_by_deal.sql @@ -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