diff --git a/models/intermediate/kpis/int_kpis__lifecycle_daily_deal.sql b/models/intermediate/kpis/int_kpis__lifecycle_daily_deal.sql index f621694..6189080 100644 --- a/models/intermediate/kpis/int_kpis__lifecycle_daily_deal.sql +++ b/models/intermediate/kpis/int_kpis__lifecycle_daily_deal.sql @@ -28,15 +28,27 @@ with booked_days_per_deal as ( select icuh.id_deal, - hd.live_date_utc as created_date_utc, - lag(hd.live_date_utc, 1) over ( - partition by icuh.id_deal order by hd.live_date_utc asc + icb.created_date_utc as created_date_utc, + lag(icb.created_date_utc, 1) over ( + partition by icuh.id_deal order by icb.created_date_utc asc ) as previous_booked_date from int_core__bookings icb inner join int_core__user_host icuh on icb.id_user_host = icuh.id_user_host inner join int_hubspot__deal hd on icuh.id_deal = hd.id_deal where icuh.id_deal is not null - group by icuh.id_deal, hd.live_date_utc + group by icuh.id_deal, icb.created_date_utc + ), + deals as ( + select + coalesce(hd.id_deal, h.id_deal) as id_deal, + min(coalesce(hd.live_date_utc, h.created_date_utc)) as created_date_utc + from intermediate.int_hubspot__deal hd + full outer join + intermediate.int_core__user_host h + on hd.id_deal = h.id_deal + and h.id_deal is not null + where hd.id_deal is not null + group by 1 ), deal_historic_booking_dates as ( select @@ -47,7 +59,7 @@ with max(b.created_date_utc) as last_time_booked_date_utc, max(b.previous_booked_date) as second_to_last_time_booked_date_utc from int_kpis__dimension_dates d - inner join int_core__user_host h on d.date >= h.created_date_utc + inner join deals h on d.date >= h.created_date_utc left join booked_days_per_deal b on h.id_deal = b.id_deal @@ -145,9 +157,7 @@ select case -- 01-New: The deal has been created this month. -- Additionally, the deal has not been offboarded in hubspot. - when - deal_was_created_this_month - and not deal_has_been_offboarded + when deal_was_created_this_month and not deal_has_been_offboarded then '01-New' -- 02-Never Booked: The deal has been created before this month and has not -- had any booking. Additionally, the deal has not been offboarded in hubspot.