diff --git a/models/intermediate/kpis/int_kpis__metric_daily_new_dash_accommodation_offered_services.sql b/models/intermediate/kpis/int_kpis__metric_daily_new_dash_accommodation_offered_services.sql index bde0477..c1ecc09 100644 --- a/models/intermediate/kpis/int_kpis__metric_daily_new_dash_accommodation_offered_services.sql +++ b/models/intermediate/kpis/int_kpis__metric_daily_new_dash_accommodation_offered_services.sql @@ -10,10 +10,21 @@ ], ) }} +with + -- Selects the most recent product bundle per accommodation per day + latest_daily_bundle as ( + select distinct on (id_accommodation, effective_start_date_utc) * + from {{ ref("int_core__accommodation_to_product_bundle") }} + order by + id_accommodation, -- Ensures we are filtering per accommodation + effective_start_date_utc desc, -- Prefers the latest effective start date + original_starts_at_utc desc, -- In case of ties, prefers the most recent original start + coalesce(original_ends_at_utc, {{ var("end_of_time") }}) desc + ) select -- Unique Key -- dd.date, - apb.id_accommodation, + ldb.id_accommodation, bs.id_user_product_bundle, bs.service_name, bs.service_business_type, @@ -35,11 +46,10 @@ inner join {{ ref("int_core__user_product_bundle_contains_services") }} as bs on uh.id_user_host = bs.id_user_host inner join - {{ ref("int_core__accommodation_to_product_bundle") }} as apb - on bs.id_user_product_bundle = apb.id_user_product_bundle - and date between apb.effective_start_date_utc and coalesce( - apb.effective_end_date_utc, {{ var("end_of_time") }} - ) + latest_daily_bundle as ldb + on bs.id_user_product_bundle = ldb.id_user_product_bundle + and date >= ldb.effective_start_date_utc + and date < coalesce(ldb.effective_end_date_utc, {{ var("end_of_time") }}) left join {{ ref("int_kpis__dimension_daily_accommodation") }} as icmas on uh.id_deal = icmas.id_deal