commit
This commit is contained in:
parent
2251a45540
commit
961b5312b2
1 changed files with 391 additions and 0 deletions
391
models/intermediate/kpis/int_kpis__product_guest_agg_metrics.sql
Normal file
391
models/intermediate/kpis/int_kpis__product_guest_agg_metrics.sql
Normal file
|
|
@ -0,0 +1,391 @@
|
||||||
|
{% set yesterday = "(current_date - interval '1 day')" %}
|
||||||
|
{% set metric_names = (
|
||||||
|
"created_guest_journeys_not_cancelled",
|
||||||
|
"started_guest_journeys_not_cancelled",
|
||||||
|
"completed_guest_journeys_not_cancelled",
|
||||||
|
"created_guest_journeys",
|
||||||
|
"started_guest_journeys",
|
||||||
|
"completed_guest_journeys",
|
||||||
|
"total_csat_score_count",
|
||||||
|
"deposit_fees_in_gbp",
|
||||||
|
"waiver_payments_in_gbp",
|
||||||
|
"checkin_cover_fees_in_gbp",
|
||||||
|
"total_guest_payments_in_gbp",
|
||||||
|
) %}
|
||||||
|
with
|
||||||
|
int_kpis__product_guest_daily_metrics as (
|
||||||
|
select
|
||||||
|
date_day,
|
||||||
|
has_payment,
|
||||||
|
has_id_check,
|
||||||
|
main_billing_country_iso_3_per_deal,
|
||||||
|
{% for metric in metric_names %}
|
||||||
|
{{ metric }}{% if not loop.last %},{% endif %}
|
||||||
|
{% endfor %}
|
||||||
|
from {{ ref("int_kpis__product_guest_daily_metrics") }}
|
||||||
|
),
|
||||||
|
date_ranges as (
|
||||||
|
select
|
||||||
|
{{ yesterday }} as current_date,
|
||||||
|
date_trunc('month', {{ yesterday }}) as current_month_start,
|
||||||
|
date_trunc('week', {{ yesterday }}) as current_week_start, -- Start of the current week
|
||||||
|
date_trunc('week', {{ yesterday }} - interval '1 year') as py_week_start, -- Start of the same week last year
|
||||||
|
date_trunc('week', {{ yesterday }} - interval '1 year')
|
||||||
|
+ interval '1 day'
|
||||||
|
* extract(
|
||||||
|
dow from {{ yesterday }} - interval '1 day'
|
||||||
|
) as py_week_current_date,
|
||||||
|
date_trunc('week', {{ yesterday }} - interval '1 week') as pp_week_start, -- Start of the same week in the previous period
|
||||||
|
date_trunc('month', {{ yesterday }} - interval '1 year') as py_month_start,
|
||||||
|
date_trunc('year', {{ yesterday }}) as current_year_start,
|
||||||
|
date_trunc('year', {{ yesterday }} - interval '1 year') as py_year_start,
|
||||||
|
date_trunc('month', {{ yesterday }} - interval '1 month') as pp_month_start,
|
||||||
|
{{ yesterday }} - interval '1 year' as py_current_date,
|
||||||
|
{{ yesterday }} - interval '1 month' as pm_current_date,
|
||||||
|
{{ yesterday }} - interval '1 week' as pw_current_date,
|
||||||
|
extract(day from {{ yesterday }}) as current_day_of_month
|
||||||
|
),
|
||||||
|
aggregated_metrics as (
|
||||||
|
{% for metric in metric_names %}
|
||||||
|
select
|
||||||
|
'{{ metric }}' as metric,
|
||||||
|
has_payment,
|
||||||
|
has_id_check,
|
||||||
|
main_billing_country_iso_3_per_deal,
|
||||||
|
'YTD' as timeframe,
|
||||||
|
sum(
|
||||||
|
case
|
||||||
|
when date_day between dr.current_year_start and {{ yesterday }}
|
||||||
|
then dm.{{ metric }}
|
||||||
|
end
|
||||||
|
) as current_value,
|
||||||
|
sum(
|
||||||
|
case
|
||||||
|
when date_day between dr.py_year_start and dr.py_current_date
|
||||||
|
then dm.{{ metric }}
|
||||||
|
end
|
||||||
|
) as py_value,
|
||||||
|
sum(
|
||||||
|
case
|
||||||
|
when date_day between dr.py_year_start and dr.py_current_date
|
||||||
|
then dm.{{ metric }}
|
||||||
|
end
|
||||||
|
) as pp_value
|
||||||
|
from int_kpis__product_guest_daily_metrics dm, date_ranges dr
|
||||||
|
where extract(day from date_day) <= dr.current_day_of_month
|
||||||
|
group by
|
||||||
|
metric, has_payment, has_id_check, main_billing_country_iso_3_per_deal
|
||||||
|
union all
|
||||||
|
select
|
||||||
|
'{{ metric }}' as metric,
|
||||||
|
has_payment,
|
||||||
|
has_id_check,
|
||||||
|
main_billing_country_iso_3_per_deal,
|
||||||
|
'MTD' as timeframe,
|
||||||
|
sum(
|
||||||
|
case
|
||||||
|
when date_day between dr.current_month_start and {{ yesterday }}
|
||||||
|
then dm.{{ metric }}
|
||||||
|
end
|
||||||
|
) as current_value,
|
||||||
|
sum(
|
||||||
|
case
|
||||||
|
when date_day between dr.py_month_start and dr.py_current_date
|
||||||
|
then dm.{{ metric }}
|
||||||
|
end
|
||||||
|
) as py_value,
|
||||||
|
sum(
|
||||||
|
case
|
||||||
|
when date_day between dr.pp_month_start and dr.pm_current_date
|
||||||
|
then dm.{{ metric }}
|
||||||
|
end
|
||||||
|
) as pp_value
|
||||||
|
from int_kpis__product_guest_daily_metrics dm, date_ranges dr
|
||||||
|
where extract(day from date_day) <= dr.current_day_of_month
|
||||||
|
group by
|
||||||
|
metric, has_payment, has_id_check, main_billing_country_iso_3_per_deal
|
||||||
|
union all
|
||||||
|
select
|
||||||
|
'{{ metric }}' as metric,
|
||||||
|
has_payment,
|
||||||
|
has_id_check,
|
||||||
|
main_billing_country_iso_3_per_deal,
|
||||||
|
'WTD' as timeframe,
|
||||||
|
sum(
|
||||||
|
case
|
||||||
|
when date_day between dr.current_week_start and {{ yesterday }}
|
||||||
|
then dm.{{ metric }}
|
||||||
|
end
|
||||||
|
) as current_value,
|
||||||
|
sum(
|
||||||
|
case
|
||||||
|
when date_day between dr.py_week_start and py_week_current_date
|
||||||
|
then dm.{{ metric }}
|
||||||
|
end
|
||||||
|
) as py_value,
|
||||||
|
sum(
|
||||||
|
case
|
||||||
|
when date_day between dr.pp_week_start and dr.pw_current_date
|
||||||
|
then dm.{{ metric }}
|
||||||
|
end
|
||||||
|
) as pp_value
|
||||||
|
from int_kpis__product_guest_daily_metrics dm, date_ranges dr
|
||||||
|
group by
|
||||||
|
metric, has_payment, has_id_check, main_billing_country_iso_3_per_deal
|
||||||
|
{% if not loop.last %}
|
||||||
|
union all
|
||||||
|
{% endif %}
|
||||||
|
{% endfor %}
|
||||||
|
),
|
||||||
|
metric_values as (
|
||||||
|
select
|
||||||
|
date_day,
|
||||||
|
has_payment,
|
||||||
|
has_id_check,
|
||||||
|
main_billing_country_iso_3_per_deal,
|
||||||
|
created_guest_journeys_not_cancelled,
|
||||||
|
completed_guest_journeys_not_cancelled,
|
||||||
|
-- Calculate the relevant sums for different timeframes
|
||||||
|
sum(
|
||||||
|
case
|
||||||
|
when date_day between dr.current_year_start and {{ yesterday }}
|
||||||
|
then created_guest_journeys_not_cancelled
|
||||||
|
end
|
||||||
|
) as ytd_created,
|
||||||
|
sum(
|
||||||
|
case
|
||||||
|
when date_day between dr.current_year_start and {{ yesterday }}
|
||||||
|
then completed_guest_journeys_not_cancelled
|
||||||
|
end
|
||||||
|
) as ytd_completed,
|
||||||
|
sum(
|
||||||
|
case
|
||||||
|
when date_day between dr.py_year_start and dr.py_current_date
|
||||||
|
then created_guest_journeys_not_cancelled
|
||||||
|
end
|
||||||
|
) as py_created,
|
||||||
|
sum(
|
||||||
|
case
|
||||||
|
when date_day between dr.py_year_start and dr.py_current_date
|
||||||
|
then completed_guest_journeys_not_cancelled
|
||||||
|
end
|
||||||
|
) as py_completed,
|
||||||
|
sum(
|
||||||
|
case
|
||||||
|
when date_day between dr.py_year_start and dr.py_current_date
|
||||||
|
then created_guest_journeys_not_cancelled
|
||||||
|
end
|
||||||
|
) as pp_created,
|
||||||
|
sum(
|
||||||
|
case
|
||||||
|
when date_day between dr.py_year_start and dr.py_current_date
|
||||||
|
then completed_guest_journeys_not_cancelled
|
||||||
|
end
|
||||||
|
) as pp_completed
|
||||||
|
from int_kpis__product_guest_daily_metrics dm
|
||||||
|
cross join date_ranges dr
|
||||||
|
where extract(day from date_day) <= dr.current_day_of_month
|
||||||
|
group by
|
||||||
|
date_day, has_payment, has_id_check, main_billing_country_iso_3_per_deal
|
||||||
|
),
|
||||||
|
calculated_metrics as (
|
||||||
|
select
|
||||||
|
'guest_journey_conversion_rate' as metric,
|
||||||
|
has_payment,
|
||||||
|
has_id_check,
|
||||||
|
main_billing_country_iso_3_per_deal,
|
||||||
|
'YTD' as timeframe,
|
||||||
|
case
|
||||||
|
when ytd_created = 0 then null else ytd_completed / ytd_created
|
||||||
|
end as current_value,
|
||||||
|
case
|
||||||
|
when py_created = 0 then null else py_completed / py_created
|
||||||
|
end as py_value,
|
||||||
|
case
|
||||||
|
when pp_created = 0 then null else pp_completed / pp_created
|
||||||
|
end as pp_value
|
||||||
|
from metric_values
|
||||||
|
)
|
||||||
|
-- select
|
||||||
|
-- 'guest_journey_conversion_rate' as metric,
|
||||||
|
-- has_payment,
|
||||||
|
-- has_id_check,
|
||||||
|
-- main_billing_country_iso_3_per_deal,
|
||||||
|
-- 'YTD' as timeframe,
|
||||||
|
-- case
|
||||||
|
-- when
|
||||||
|
-- sum(
|
||||||
|
-- case
|
||||||
|
-- when
|
||||||
|
-- date_day
|
||||||
|
-- between dr.current_year_start and {{ yesterday }}
|
||||||
|
-- then created_guest_journeys_not_cancelled
|
||||||
|
-- end
|
||||||
|
-- )
|
||||||
|
-- = 0
|
||||||
|
-- then null
|
||||||
|
-- else
|
||||||
|
-- sum(
|
||||||
|
-- case
|
||||||
|
-- when
|
||||||
|
-- date_day
|
||||||
|
-- between dr.current_year_start and {{ yesterday }}
|
||||||
|
-- then completed_guest_journeys_not_cancelled
|
||||||
|
-- end
|
||||||
|
-- ) / sum(
|
||||||
|
-- case
|
||||||
|
-- when
|
||||||
|
-- date_day
|
||||||
|
-- between dr.current_year_start and {{ yesterday }}
|
||||||
|
-- then created_guest_journeys_not_cancelled
|
||||||
|
-- end
|
||||||
|
-- )
|
||||||
|
-- end as current_value,
|
||||||
|
-- case
|
||||||
|
-- when
|
||||||
|
-- sum(
|
||||||
|
-- case
|
||||||
|
-- when
|
||||||
|
-- date_day
|
||||||
|
-- between dr.py_year_start and dr.py_current_date
|
||||||
|
-- then created_guest_journeys_not_cancelled
|
||||||
|
-- end
|
||||||
|
-- )
|
||||||
|
-- = 0
|
||||||
|
-- then null
|
||||||
|
-- else
|
||||||
|
-- sum(
|
||||||
|
-- case
|
||||||
|
-- when
|
||||||
|
-- date_day
|
||||||
|
-- between dr.py_year_start and dr.py_current_date
|
||||||
|
-- then completed_guest_journeys_not_cancelled
|
||||||
|
-- end
|
||||||
|
-- ) / sum(
|
||||||
|
-- case
|
||||||
|
-- when
|
||||||
|
-- date_day
|
||||||
|
-- between dr.py_year_start and dr.py_current_date
|
||||||
|
-- then created_guest_journeys_not_cancelled
|
||||||
|
-- end
|
||||||
|
-- ) as py_value,
|
||||||
|
-- case
|
||||||
|
-- when
|
||||||
|
-- sum(
|
||||||
|
-- case
|
||||||
|
-- when
|
||||||
|
-- date_day
|
||||||
|
-- between dr.py_year_start and dr.py_current_date
|
||||||
|
-- then created_guest_journeys_not_cancelled
|
||||||
|
-- end
|
||||||
|
-- )
|
||||||
|
-- = 0
|
||||||
|
-- then null
|
||||||
|
-- else
|
||||||
|
-- sum(
|
||||||
|
-- case
|
||||||
|
-- when
|
||||||
|
-- date_day
|
||||||
|
-- between dr.py_year_start and dr.py_current_date
|
||||||
|
-- then completed_guest_journeys_not_cancelled
|
||||||
|
-- end
|
||||||
|
-- ) / sum(
|
||||||
|
-- case
|
||||||
|
-- when
|
||||||
|
-- date_day
|
||||||
|
-- between dr.py_year_start and dr.py_current_date
|
||||||
|
-- then created_guest_journeys_not_cancelled
|
||||||
|
-- end
|
||||||
|
-- ) as pp_value
|
||||||
|
-- from int_kpis__product_guest_daily_metrics dm, date_ranges dr
|
||||||
|
-- where extract(day from date_day) <= dr.current_day_of_month
|
||||||
|
-- group by metric, has_payment, has_id_check, main_billing_country_iso_3_per_deal
|
||||||
|
-- union all
|
||||||
|
-- select
|
||||||
|
-- 'guest_revenue_per_guest_journey_created' as metric,
|
||||||
|
-- has_payment,
|
||||||
|
-- has_id_check,
|
||||||
|
-- main_billing_country_iso_3_per_deal,
|
||||||
|
-- 'YTD' as timeframe,
|
||||||
|
-- sum(
|
||||||
|
-- case
|
||||||
|
-- when date_day between dr.current_year_start and {{ yesterday }}
|
||||||
|
-- then total_guest_payments_in_gbp
|
||||||
|
-- end
|
||||||
|
-- ) / sum(
|
||||||
|
-- case
|
||||||
|
-- when date_day between dr.current_year_start and {{ yesterday }}
|
||||||
|
-- then created_guest_journeys_not_cancelled
|
||||||
|
-- end
|
||||||
|
-- ) as current_value,
|
||||||
|
-- sum(
|
||||||
|
-- case
|
||||||
|
-- when date_day between dr.py_year_start and dr.py_current_date
|
||||||
|
-- then total_guest_payments_in_gbp
|
||||||
|
-- end
|
||||||
|
-- ) / sum(
|
||||||
|
-- case
|
||||||
|
-- when date_day between dr.py_year_start and dr.py_current_date
|
||||||
|
-- then completed_guest_journeys_not_cancelled
|
||||||
|
-- end
|
||||||
|
-- ) as py_value,
|
||||||
|
-- sum(
|
||||||
|
-- case
|
||||||
|
-- when date_day between dr.py_year_start and dr.py_current_date
|
||||||
|
-- then total_guest_payments_in_gbp
|
||||||
|
-- end
|
||||||
|
-- ) / sum(
|
||||||
|
-- case
|
||||||
|
-- when date_day between dr.py_year_start and dr.py_current_date
|
||||||
|
-- then completed_guest_journeys_not_cancelled
|
||||||
|
-- end
|
||||||
|
-- ) as pp_value
|
||||||
|
-- from int_kpis__product_guest_daily_metrics dm, date_ranges dr
|
||||||
|
-- where extract(day from date_day) <= dr.current_day_of_month
|
||||||
|
-- group by metric, has_payment, has_id_check, main_billing_country_iso_3_per_deal
|
||||||
|
-- union all
|
||||||
|
-- select
|
||||||
|
-- 'csat_score' as metric,
|
||||||
|
-- has_payment,
|
||||||
|
-- has_id_check,
|
||||||
|
-- main_billing_country_iso_3_per_deal,
|
||||||
|
-- 'YTD' as timeframe,
|
||||||
|
-- sum(
|
||||||
|
-- case
|
||||||
|
-- when date_day between dr.current_year_start and {{ yesterday }}
|
||||||
|
-- then total_csat_score_count * average_csat_score
|
||||||
|
-- end
|
||||||
|
-- ) / sum(
|
||||||
|
-- case
|
||||||
|
-- when date_day between dr.current_year_start and {{ yesterday }}
|
||||||
|
-- then total_csat_score_count
|
||||||
|
-- end
|
||||||
|
-- ) as current_value,
|
||||||
|
-- sum(
|
||||||
|
-- case
|
||||||
|
-- when date_day between dr.py_year_start and dr.py_current_date
|
||||||
|
-- then total_csat_score_count * average_csat_score
|
||||||
|
-- end
|
||||||
|
-- ) / sum(
|
||||||
|
-- case
|
||||||
|
-- when date_day between dr.py_year_start and dr.py_current_date
|
||||||
|
-- then total_csat_score_count
|
||||||
|
-- end
|
||||||
|
-- ) as py_value,
|
||||||
|
-- sum(
|
||||||
|
-- case
|
||||||
|
-- when date_day between dr.py_year_start and dr.py_current_date
|
||||||
|
-- then total_csat_score_count * average_csat_score
|
||||||
|
-- end
|
||||||
|
-- ) / sum(
|
||||||
|
-- case
|
||||||
|
-- when date_day between dr.py_year_start and dr.py_current_date
|
||||||
|
-- then total_csat_score_count
|
||||||
|
-- end
|
||||||
|
-- ) as pp_value
|
||||||
|
-- from int_kpis__product_guest_daily_metrics dm, date_ranges dr
|
||||||
|
-- where extract(day from date_day) <= dr.current_day_of_month
|
||||||
|
-- group by metric, has_payment, has_id_check, main_billing_country_iso_3_per_deal
|
||||||
|
-- )
|
||||||
|
select *
|
||||||
|
from aggregated_metrics
|
||||||
Loading…
Add table
Add a link
Reference in a new issue