diff --git a/orchestration/dags/data_gcp_dbt/models/intermediate/metric/diversity/int_metric__diversity_daily_booking.sql b/orchestration/dags/data_gcp_dbt/models/intermediate/metric/diversity/int_metric__diversity_daily_booking.sql index 9caa56f146..397e20231f 100644 --- a/orchestration/dags/data_gcp_dbt/models/intermediate/metric/diversity/int_metric__diversity_daily_booking.sql +++ b/orchestration/dags/data_gcp_dbt/models/intermediate/metric/diversity/int_metric__diversity_daily_booking.sql @@ -8,11 +8,27 @@ }} {% set entities = [ - {"entity": "offer_category_id", "type": "OFFER_CATEGORY", "score_multiplier": 25}, - {"entity": "venue_type_label", "type": "VENUE_TYPE", "score_multiplier": 20}, - {"entity": "offer_subcategory_id", "type": "OFFER_SUBCATEGORY", "score_multiplier": 10}, + { + "entity": "offer_category_id", + "type": "OFFER_CATEGORY", + "score_multiplier": 25, + }, + { + "entity": "venue_type_label", + "type": "VENUE_TYPE", + "score_multiplier": 20, + }, + { + "entity": "offer_subcategory_id", + "type": "OFFER_SUBCATEGORY", + "score_multiplier": 10, + }, {"entity": "venue_id", "type": "VENUE", "score_multiplier": 5}, - {"entity": "extra_category", "type": "EXTRA_CATEGORY", "score_multiplier": 5}, + { + "entity": "extra_category", + "type": "EXTRA_CATEGORY", + "score_multiplier": 5, + }, ] %} with @@ -26,31 +42,35 @@ with venue_type_label, offer_category_id, venue_id, - coalesce(offer_type_label, venue_id) as extra_category, -- venue_id is used as extra_category when offer_type_label is null - row_number() over (partition by user_id order by booking_created_at) as booking_rank + coalesce(offer_type_label, venue_id) as extra_category, -- venue_id is used as extra_category when offer_type_label is null + row_number() over ( + partition by user_id order by booking_created_at + ) as booking_rank from {{ ref("int_global__booking") }} where booking_status != 'CANCELLED' -), - -entity_calculations as ( - {% for entity in entities %} - select distinct - booking_id, - booking_created_at, - booking_creation_date, - booking_rank, - user_id, - {{ entity.entity }} as diversity_booked_entity, - '{{ entity.type }}' as diversity_booked_entity_type, - {{ entity.score_multiplier }} as score_multiplier, - row_number() over (partition by user_id, {{ entity.entity }} order by booking_created_at) as diversity_booking_entity_rank - from raw_data - {% if not loop.last %} - union all - {% endif %} - {% endfor %} -) + ), + entity_calculations as ( + {% for entity in entities %} + select distinct + booking_id, + booking_created_at, + booking_creation_date, + booking_rank, + user_id, + {{ entity.entity }} as diversity_booked_entity, + '{{ entity.type }}' as diversity_booked_entity_type, + {{ entity.score_multiplier }} as score_multiplier, + row_number() over ( + partition by user_id, {{ entity.entity }} + order by booking_created_at + ) as diversity_booking_entity_rank + from raw_data + {% if not loop.last %} + union all + {% endif %} + {% endfor %} + ) select booking_rank, @@ -62,9 +82,10 @@ select user_id, booking_creation_date, case - when booking_rank = 1 then score_multiplier - when diversity_booking_entity_rank = 1 then score_multiplier - else 0 - end - as diversity_score + when booking_rank = 1 + then score_multiplier + when diversity_booking_entity_rank = 1 + then score_multiplier + else 0 + end as diversity_score from entity_calculations