From 189e77dd76c505038df8795d14839e2ce7ff506c Mon Sep 17 00:00:00 2001 From: Joaquin Ossa Date: Thu, 29 Aug 2024 09:30:36 +0200 Subject: [PATCH] fixed variable definitions and added comments for currency-less fees --- .../core/int_edeposit__agg_fee_per_user.sql | 41 ++++++++++++------- models/intermediate/core/schema.yaml | 15 ++++--- 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/models/intermediate/core/int_edeposit__agg_fee_per_user.sql b/models/intermediate/core/int_edeposit__agg_fee_per_user.sql index f65b316..0a61c9c 100644 --- a/models/intermediate/core/int_edeposit__agg_fee_per_user.sql +++ b/models/intermediate/core/int_edeposit__agg_fee_per_user.sql @@ -1,8 +1,8 @@ -{% set ok_status_fee_calculation = "case when verification_status = 'Approved' or verification_status = 'Flagged' then cast(nightly_fee_local as float) * number_nights else 0 end" %} - -{% set rejected_fee_calculation = ( - "case when verification_status = 'Rejected' then 0.25 else 0 end" -) %} +{% set ok_status = ("Approved", "Flagged") %} +{% set rejected_status = "Rejected" %} +{% set rejected_fee = 0.25 %} +{% set cancellation_fee = 0.25 %} +{% set cancellation_threshold = 0.05 %} with int_edeposit__verifications as ( select * from {{ ref("int_edeposit__verifications") }} @@ -17,8 +17,16 @@ with creation_at_utc, verification_status, id_user_host, - {{ ok_status_fee_calculation }} as ok_status_fee, - {{ rejected_fee_calculation }} as rejected_fee, + case + when verification_status in {{ ok_status }} + then cast(nightly_fee_local as float) * number_nights + else 0 + end as ok_status_fee, + case + when verification_status = '{{ rejected_status }}' + then {{ rejected_fee }} + else 0 + end as rejected_fee, to_char(creation_at_utc, 'YYYY-MM') as year_month_created, to_char(checkout_at_utc, 'YYYY-MM') as year_month_checkout from int_edeposit__verifications @@ -30,17 +38,22 @@ select id_user_host, count(distinct id_booking) as bookings_per_month, sum(cast(is_cancelled as integer)) as cancelled_per_month, - sum(cast(is_cancelled as integer))::decimal / count(*) as cancelled_ratio, + sum(cast(is_cancelled as integer))::decimal + / count(distinct id_booking) as cancelled_ratio, case - when sum(cast(is_cancelled as integer))::decimal / count(*) >= 0.05 - then sum(cast(is_cancelled as integer)) * 0.25 + when + sum(cast(is_cancelled as integer))::decimal / count(distinct id_booking) + >= {{ cancellation_threshold }} + then sum(cast(is_cancelled as integer)) * {{ cancellation_fee }} else 0 end as sum_cancelled_fee, - sum(ok_status_fee) as ok_status_fee_sum, - sum(rejected_fee) as rejected_fee_sum, + sum(ok_status_fee) as sum_ok_status_fee, + sum(rejected_fee) as sum_rejected_fee, case - when sum(cast(is_cancelled as integer))::decimal / count(*) >= 0.05 - then sum(cast(is_cancelled as integer)) * 0.25 + when + sum(cast(is_cancelled as integer))::decimal / count(distinct id_booking) + >= {{ cancellation_threshold }} + then sum(cast(is_cancelled as integer)) * {{ cancellation_fee }} else 0 end + sum(ok_status_fee) diff --git a/models/intermediate/core/schema.yaml b/models/intermediate/core/schema.yaml index 9261bb4..74de0e0 100644 --- a/models/intermediate/core/schema.yaml +++ b/models/intermediate/core/schema.yaml @@ -2979,17 +2979,20 @@ models: - name: sum_cancelled_fee data_type: numeric - description: "sum of fees charged for cancelled bookings" + description: "sum of fees charged for cancelled bookings + (currency-less)" - - name: ok_status_fee_sum + - name: sum_ok_status_fee data_type: double precision description: | - "sum of fees charged for bookings with status 'Approved' or 'Flagged'" + "sum of fees charged for bookings with status 'Approved' or 'Flagged' + (currency-less)" - - name: rejected_fee_sum + - name: sum_rejected_fee data_type: numeric - description: "sum of fees charged for rejected bookings" + description: "sum of fees charged for rejected bookings + (currency-less)" - name: total_revenue data_type: double precision - description: "total sum of fees charged" \ No newline at end of file + description: "total sum of fees charged (currency-less)" \ No newline at end of file